package cn.stylefeng.guns.modular.order.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.stylefeng.guns.core.util.justGoodsInfoApi;
import cn.stylefeng.guns.modular.order.service.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.ExcelWrite;
import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.entity.BasicGoods;
import cn.stylefeng.guns.modular.basic.entity.BasicWarehouse;
import cn.stylefeng.guns.modular.basic.model.params.BasicGoodsParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicPrivatecarParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicTrayParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicGoodsResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicPrivatecarResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicTrayResult;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicGoodsService;
import cn.stylefeng.guns.modular.basic.service.BasicPrivatecarService;
import cn.stylefeng.guns.modular.basic.service.BasicTrayService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.order.entity.OrderPut;
import cn.stylefeng.guns.modular.order.entity.OrderPutExcelItem;
import cn.stylefeng.guns.modular.order.mapper.OrderPutMapper;
import cn.stylefeng.guns.modular.order.model.params.OrderPutDetailedParam;
import cn.stylefeng.guns.modular.order.model.params.OrderPutParam;
import cn.stylefeng.guns.modular.order.model.params.OrderTransportDetailedParam;
import cn.stylefeng.guns.modular.order.model.params.OrderTransportParam;
import cn.stylefeng.guns.modular.order.model.result.OrderPutDetailedResult;
import cn.stylefeng.guns.modular.order.model.result.OrderPutResult;
import cn.stylefeng.guns.modular.order.model.result.OrderTransportDetailedResult;
import cn.stylefeng.guns.modular.order.model.result.OrderTransportResult;
import cn.stylefeng.guns.modular.storage.entity.StorageStock;
import cn.stylefeng.guns.modular.storage.model.params.StoragePutParam;
import cn.stylefeng.guns.modular.storage.model.params.StoragePutSingleParam;
import cn.stylefeng.guns.modular.storage.model.params.StorageStockParam;
import cn.stylefeng.guns.modular.storage.model.result.StoragePutResult;
import cn.stylefeng.guns.modular.storage.model.result.StoragePutSingleResult;
import cn.stylefeng.guns.modular.storage.model.result.StorageStockResult;
import cn.stylefeng.guns.modular.storage.service.StoragePutService;
import cn.stylefeng.guns.modular.storage.service.StoragePutSingleService;
import cn.stylefeng.guns.modular.storage.service.StorageStockDetailedService;
import cn.stylefeng.guns.modular.storage.service.StorageStockService;
import cn.stylefeng.guns.modular.transport.model.params.TDispatchParam;
import cn.stylefeng.guns.modular.transport.model.params.TransportDispatchParam;
import cn.stylefeng.guns.modular.transport.model.params.TransportReceiptParam;
import cn.stylefeng.guns.modular.transport.model.result.TDispatchResult;
import cn.stylefeng.guns.modular.transport.model.result.TransportDispatchResult;
import cn.stylefeng.guns.modular.transport.model.result.TransportReceiptResult;
import cn.stylefeng.guns.modular.transport.service.TDispatchService;
import cn.stylefeng.guns.modular.transport.service.TransportDispatchService;
import cn.stylefeng.guns.modular.transport.service.TransportReceiptService;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

import static cn.hutool.poi.excel.sax.AttributeName.s;

/**
 * 入库订单表 服务实现类
 */
@Service
public class OrderPutServiceImpl extends ServiceImpl<OrderPutMapper, OrderPut> implements OrderPutService {

    @Resource
    private BasicCustomerService basicCustomerService;

    @Resource
    private BasicWarehouseService basicWarehouseService;

    @Resource
    private BasicGoodsService basicGoodsService;

    @Resource
    private UserService userService;

    @Resource
    private OrderPutDetailedService orderPutDetailedService;

    @Resource
    private StoragePutService storagePutService;

    @Resource
    private StoragePutSingleService storagePutSingleService;
    
    @Resource
    private StorageStockService storageStockService;

    @Resource
    private OrderTransportDetailedService orderTransportDetailedService;

	@Resource
    private TransportDispatchService transportDispatchService;

    @Resource
    private BasicPrivatecarService basicPrivatecarService;

    @Resource
    private OrderTransportService orderTransportService;

    @Resource
    private TDispatchService tDispatchService;
    
    @Resource
    private TransportReceiptService transportReceiptService;

    @Resource
    private BasicTrayService basicTrayService;
	@Resource
    private JSTPutOrderScheduledService jstPutOrderScheduledService;

    @Override
    public void add(OrderPutParam param){
        OrderPut entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(OrderPutParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(OrderPutParam param){
        OrderPut oldEntity = getOldEntity(param);
        OrderPut newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public OrderPutResult findBySpec(OrderPutParam param,Long operateId){
		return this.baseMapper.findBySpec(param, operateId);
    }

    @Override
    public List<OrderPutResult> findListBySpec(OrderPutParam param,Long operateId){
        return this.baseMapper.findListBySpec(param,operateId);
    }

    @Override
    public LayuiPageInfo findPageBySpec(OrderPutParam param,String condition,Long operateId){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(OrderPutParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private OrderPut getOldEntity(OrderPutParam param) {
        return this.getById(getKey(param));
    }

    private OrderPut getEntity(OrderPutParam param) {
        OrderPut entity = new OrderPut();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

	@Override
	public OrderPut findByThelastone() {
		return this.baseMapper.findByThelastone();
	}

	@Override
	public boolean checkCustomerOrder(String putCustomerNumber) {
		return this.baseMapper.checkCustomerOrder(putCustomerNumber)==null?false:true;
	}
	

	/** 
    * 获取当前日期 yyyy-MM-dd
    * @param past 
    * @return 
    */  
   public static String getDatesssss() {  
       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
       return format.format(new Date());  
   }   

	@Override
	public LayuiPageInfo querylistPageBySpec(OrderPutParam param, String condition,Long operateId) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.querylistPageBySpec(pageContext, param,condition, operateId);
        return LayuiPageFactory.createPageInfo(page);
	}

	@Override
	public boolean saveBatchList(List<OrderPutParam> list) {
		return this.baseMapper.saveBatchList(list);
	}
	
	private static String getCellValue(Cell cell) {
        if (cell == null) {
            return "";
        }
        if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            if (DateUtil.isCellDateFormatted(cell)) {
                return HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString();
            } else {
                return new BigDecimal(Double.toString(cell.getNumericCellValue())).toString();
            }
        } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
            return StringUtils.trimToEmpty(cell.getStringCellValue());
        } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
            return StringUtils.trimToEmpty(cell.getCellFormula());
        } else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
            return "";
        } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
            return String.valueOf(cell.getBooleanCellValue());
        } else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {
            return "ERROR";
        } else {
            return cell.toString().trim();
        }
    }

	@Resource
    private DeptService deptService;
	
	/**
	 * 获取所属项目单号前缀
	 * @param user
	 * @return
	 */
	public String getOperateIdOrderPrefix(User user) {
		if(user.getOperateId() != null) {
			Dept dept = new Dept();
			dept.setOperateId(user.getOperateId());
			Dept resdept = deptService.findBySpec(dept);
			if(resdept != null) {
				if(ToolUtil.isNotEmpty(resdept.getOrderPrefix())) {
					return resdept.getOrderPrefix();
				} else {
					return user.getAccount().substring(0, 3);
				}
			} else {
				return null;
			}
		} else {
			return null;
		}
	}
	
	/**
	 * 处理入库订单导入
	 */
	@Override
	@Transactional(rollbackFor = Exception.class,timeout=80)
	public ResponseData getExceldata(MultipartFile file,OrderPutParam orderPutParam, ResponseData data) throws IOException {

		String fileName = file.getOriginalFilename();
		List<OrderPutExcelItem> list = new ArrayList<OrderPutExcelItem>();
		
		if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
			data.setMessage("Incorrect upload file format");
			data.setSuccess(false);
        }

		boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        Sheet sheet = wb.getSheetAt(0);
        
        for (int r = 1; r <= sheet.getLastRowNum()+1; r++) {
        	Row row = sheet.getRow(r);
            if (row == null){
                continue;
            }
            OrderPutExcelItem excelItem = new OrderPutExcelItem();

            String  carNumber = getCellValue(row.getCell(0));
            if (ToolUtil.isNotEmpty(carNumber)) {
                excelItem.setCarNumber(carNumber);//车牌号
			}

            String goodsNumber = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(1));
            if (ToolUtil.isNotEmpty(goodsNumber)) {
                excelItem.setGoodsNumber(goodsNumber);//商品编号
			}

            String goodsName = getCellValue(row.getCell(2));
            if (ToolUtil.isNotEmpty(goodsName)) {
                excelItem.setGoodsName(goodsName);//商品名称
			}

            String batchnumber = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(3));
            if (ToolUtil.isNotEmpty(batchnumber)) {
                excelItem.setBatchnumber(batchnumber);//生产日期
			}

            String putnum = getCellValue(row.getCell(4));
            if (ToolUtil.isNotEmpty(putnum)) {
            	BigDecimal as = new BigDecimal(0);
            	BigDecimal putnumb = new BigDecimal(putnum);
            	if(putnumb.compareTo(as) != 1) {
            		continue;
            	}
                excelItem.setPutnum(putnum);//应入数量
			}

            String detailedMoney = getCellValue(row.getCell(5));
            if (ToolUtil.isNotEmpty(detailedMoney)) {
                excelItem.setDetailedMoney(detailedMoney);//金额
			}

            String putvolume = getCellValue(row.getCell(6));
            if (ToolUtil.isNotEmpty(putvolume)) {
                excelItem.setPutvolume(putvolume);//应入体积
			}

            String shipmentnumber = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(7));
            if (ToolUtil.isNotEmpty(shipmentnumber)) {
                excelItem.setShipmentnumber(shipmentnumber);//客户货运订单号   (客户单号){
			}

            String putCustomerNumber = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(8));
            if (ToolUtil.isNotEmpty(putCustomerNumber)) {
                excelItem.setPutCustomerNumber(putCustomerNumber);//客户交货单号
			}

            String goodsType = getCellValue(row.getCell(9));
            if (ToolUtil.isNotEmpty(goodsType)) {
                excelItem.setGoodsType(goodsType);//品类描述
			}

            String goodsUnit = getCellValue(row.getCell(10));
            if (ToolUtil.isNotEmpty(goodsUnit)) {
                excelItem.setGoodsUnit(goodsUnit);//基本单位
			}

            String recommend = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(11));
            if (ToolUtil.isNotEmpty(recommend)) {
                excelItem.setRecommend(recommend);//推荐批次
			}

            String notes = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(12));
            if (ToolUtil.isNotEmpty(notes)) {
                excelItem.setNotes(notes);
			}

            String goodsbatchs = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(13));
            if (ToolUtil.isNotEmpty(goodsbatchs)) {
                excelItem.setGoodsbatchs(goodsbatchs);
			}

            String goodsSpecs = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(14));
            if (ToolUtil.isNotEmpty(goodsSpecs)) {
                excelItem.setGoodsSpecs(goodsSpecs);
			}
            
            list.add(excelItem);
        }

		if(orderPutParam.getWarehouseId()==null||orderPutParam.getPutCustomerId()==null) {
			data.setSuccess(false);
			data.setMessage("Please fill in the order basic information!");
			return data;
		}
		List<OrderPutParam> orderPutList = new ArrayList<OrderPutParam>();//批量保存的 入库订单list
		List<OrderPutDetailedParam> detailedList = new ArrayList<OrderPutDetailedParam>();//批量保存的 订单商品明细list
		BasicCustomer basicCustomer = basicCustomerService.getById(orderPutParam.getPutCustomerId());//客户信息
		BasicWarehouse basicWarehouse = basicWarehouseService.getById(orderPutParam.getWarehouseId());//仓库信息
		//查询最新 入库订单
    	OrderPut orderPut = this.findByThelastone();
    	int putNumber = 1;
    	if (orderPut!=null) {
    		putNumber = orderPut.getId()+1;
		} 
    	boolean newgoodsflag = false;
    	String goodsnumbers = "";
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
		String trayCode = getOperateIdOrderPrefix(user);
		for (OrderPutExcelItem excelItem : list) {
			if (ToolUtil.isEmpty(excelItem.getGoodsNumber())&&ToolUtil.isEmpty(excelItem.getGoodsName())) {
				break;
			} 
			if(ToolUtil.isEmpty(excelItem.getGoodsNumber())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
				data.setSuccess(false);
				data.setMessage("Product code cannot be empty in imported data!");
				return data;
			}
			if(ToolUtil.isEmpty(excelItem.getPutnum())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
				data.setSuccess(false);
				data.setMessage("Inbound quantity cannot be empty in imported data!");
				return data;
			}
			//通过 导入的客户单号 查询是否存在
			OrderPutParam opp = new OrderPutParam();
			boolean whether2 = false;
			if(ToolUtil.isEmpty(excelItem.getShipmentnumber())) {
				whether2 = true;
				excelItem.setShipmentnumber(trayCode+"-"+"RK-"+cn.stylefeng.guns.core.util.DateUtil.getDateBch()+"-"+(putNumber));
			}
			opp.setPutCustomerNumber(excelItem.getShipmentnumber());//客户单号 (客户货运订单号)
			OrderPutResult oprOrderPutResult = this.findBySpec(opp,null);//状态不等于 作废
			//如果不存在
			if(oprOrderPutResult==null) {
				//新增入库订单
				opp.setPutNumber(trayCode+"-"+"RK-"+cn.stylefeng.guns.core.util.DateUtil.getDateBch()+"-"+(putNumber));
		    	opp.setPutCustomerId(orderPutParam.getPutCustomerId());//客户id
		    	opp.setPutCustomerName(basicCustomer.getCustomerName());//客户名称
		    	opp.setSettlementunit(basicCustomer.getCustomerSettlement());//结算单位
		    	opp.setWarehouseId(orderPutParam.getWarehouseId());//仓库id
		    	opp.setWarehouseName(basicWarehouse.getWarehouseName());//仓库名称
		    	opp.setPutType("PICKUP_STORAGE");//入库类别 默认为‘提货入库’
		    	opp.setPutMode("WINDOW_RECEIVING");//入库方式 默认为‘窗口接货’  不生成货运订单
		    	opp.setSettlement(orderPutParam.getSettlement());//结算方式
		    	opp.setSettlementunit(null);
		    	opp.setCarnumber(excelItem.getCarNumber());//车牌号
		    	opp.setShipmentnumber(excelItem.getPutCustomerNumber());//客户交货单号
		    	opp.setBillstate("PENDING_SUBMIT");//订单状态
		    	opp.setCreateName(user.getName());//制单人名称
		    	opp.setCreateTime(new Date());
		    	opp.setCreateUser(user.getUserId());
		    	opp.setNotes(excelItem.getNotes());
		    	opp.setDeliverId(deliverId);//交付id
		    	opp.setDeptId(deptId);//部门id
		    	boolean whether = false;//判断客户单号  是否重复
		    	for (OrderPutParam opp2 : orderPutList) {
					if (opp2.getPutCustomerNumber().equals(opp.getPutCustomerNumber())) {
						whether = true;
				    	oprOrderPutResult = new OrderPutResult();
		    			ToolUtil.copyProperties(opp2,oprOrderPutResult);
					}
				}
		    	if (!whether) {
		    		orderPutList.add(opp);
		    		if(!whether2) {
		    			putNumber++;
		    		}
			    	oprOrderPutResult = new OrderPutResult();
	    			ToolUtil.copyProperties(opp,oprOrderPutResult);
				}
    			opp = new OrderPutParam();
			} else {
				//如果 导入的客户单号 已存在
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
				data.setSuccess(false);
				data.setMessage("Customer order number: "+excelItem.getShipmentnumber()+" already exists in imported data!");
				return data;
			}
			
			//通过商品编号 查询商品是否存在
			BasicGoodsParam bgpParam = new BasicGoodsParam();
			bgpParam.setGoodsNumber(excelItem.getGoodsNumber());
			bgpParam.setGoodsCustomerId(Integer.parseInt(orderPutParam.getPutCustomerId().toString()));
			BasicGoodsResult bgrBasicGoodsResult = basicGoodsService.findBySpec(bgpParam,null);
			//计算 单价，体积单位
			BigDecimal putnum = new BigDecimal(excelItem.getPutnum()).setScale(3, BigDecimal.ROUND_HALF_DOWN);//应入数量
			BigDecimal putvolume =  new BigDecimal(0);//应入体积
			BigDecimal v = new BigDecimal(0);//体积单位
			BigDecimal u = new BigDecimal(0);//单价
			if (ToolUtil.isNotEmpty(excelItem.getPutvolume())) {
				putvolume =  new BigDecimal(excelItem.getPutvolume()).setScale(3, BigDecimal.ROUND_HALF_DOWN);
				v = putvolume.divide(putnum,8,BigDecimal.ROUND_HALF_DOWN);//(四舍五入保留8位)
			} else {
				if (bgrBasicGoodsResult != null && ToolUtil.isNotEmpty(bgrBasicGoodsResult.getGoodsVolume())) {
					v = new BigDecimal(bgrBasicGoodsResult.getGoodsVolume()).setScale(8, BigDecimal.ROUND_HALF_DOWN);//(四舍五入保留8位)
					putvolume =  putnum.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN);
				} else {
					v =  new BigDecimal("0.1");
				}
			}
			BigDecimal detailedMoney = new BigDecimal(0);//金额
			if (ToolUtil.isNotEmpty(excelItem.getDetailedMoney())) {
				detailedMoney = new BigDecimal(excelItem.getDetailedMoney()).setScale(3, BigDecimal.ROUND_HALF_DOWN);
				u = detailedMoney.divide(putnum,3,BigDecimal.ROUND_HALF_DOWN);//(四舍五入保留3位)
			} else {
				if (bgrBasicGoodsResult != null && ToolUtil.isNotEmpty(bgrBasicGoodsResult.getGoodsCharging())) {
					u = new BigDecimal(bgrBasicGoodsResult.getGoodsCharging()).setScale(3, BigDecimal.ROUND_HALF_DOWN);
					detailedMoney = putnum.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN);
				} else {
					u = new BigDecimal("0.1");
				}
			}
			BigDecimal as = new BigDecimal("0");
			//如果商品不存在  新增基础信息
			if(bgrBasicGoodsResult==null){
				bgpParam.setGoodsCustomerName(basicCustomer.getCustomerName());//客户名称
				bgpParam.setGoodsName(excelItem.getGoodsName());//商品名称
				bgpParam.setGoodsNo(excelItem.getGoodsNumber());
				bgpParam.setGoodsUnit(excelItem.getGoodsUnit());//单位 (基本单位)
				bgpParam.setGoodsBigtype(excelItem.getGoodsType());//商品大类 (品类描述)
				bgpParam.setGoodsWeight("0.1");//重量单位 默认1
				if (v.compareTo(as)==1) {//大于 返回1
					bgpParam.setGoodsVolume(v.toString());//体积
				} else {
					bgpParam.setGoodsVolume("0.1");//默认1
				}
				if(u.compareTo(as)==1) {//大于 返回1
					bgpParam.setGoodsCharging(u.toString());//计费指定价
				} else {
					bgpParam.setGoodsCharging("10");//默认1
				}
				bgpParam.setGoodsBottom("10");//底
				bgpParam.setGoodsLamination("10");//叠层
				bgpParam.setGoodsSupportvolume("100");//每托存量 默认100
				bgpParam.setGoodsTraylayer("1");//托层 默认1
				bgpParam.setGoodsWarranty("36");
				bgpParam.setGoodsSpecs(excelItem.getGoodsSpecs());
				bgpParam.setDeliverId(deliverId);
				bgpParam.setDeptId(deptId);
				bgpParam.setCreateTime(new Date());
				basicGoodsService.addReturnId(bgpParam);
				bgrBasicGoodsResult = new BasicGoodsResult();
				ToolUtil.copyProperties(bgpParam, bgrBasicGoodsResult);
				newgoodsflag = true;
				goodsnumbers += excelItem.getGoodsNumber()+";";
			} else {
				boolean flag = false;//是否更新商品
				if (ToolUtil.isNotEmpty(bgrBasicGoodsResult.getGoodsVolume())) {
					BigDecimal volume =  new BigDecimal(bgrBasicGoodsResult.getGoodsVolume());
					if(v.compareTo(volume) != 0) {
						bgrBasicGoodsResult.setGoodsVolume(v.toString());
						flag = true;
					}
				} else {
					bgrBasicGoodsResult.setGoodsVolume(v.toString());
					flag = true;
				}
				if (ToolUtil.isNotEmpty(bgrBasicGoodsResult.getGoodsCharging())) {
					BigDecimal charging =  new BigDecimal(bgrBasicGoodsResult.getGoodsCharging());
					if(u.compareTo(charging) != 0) {
						bgrBasicGoodsResult.setGoodsCharging(u.toString());
						flag = true;
					}
				} else {
					bgrBasicGoodsResult.setGoodsCharging(u.toString());
					flag = true;
				}
				if (flag) {
					ToolUtil.copyProperties(bgrBasicGoodsResult, bgpParam);
					basicGoodsService.update(bgpParam);
				}
			}
			BigDecimal goodsWeight = new BigDecimal(bgrBasicGoodsResult.getGoodsWeight());//重量单位
			String batchnumber = excelItem.getBatchnumber();//导入生产日期
			if(ToolUtil.isEmpty(batchnumber)){
				batchnumber = cn.stylefeng.guns.core.util.DateUtil.getDateBch();
			}
			char[] dataArray = batchnumber.toCharArray();
			StringBuffer strBuffer = new StringBuffer();
			if(batchnumber.length()==10) {
				batchnumber = "20"+batchnumber.substring(0,6);//恒安导入 2111120456
			} else if(batchnumber.length()==19){
				batchnumber = batchnumber.substring(0, 10).replace("-","");
			} else {
				batchnumber = batchnumber.substring(0, 8);//贝因美导入 20210613
			}
			dataArray = batchnumber.toCharArray();
			for(int i = 0; i < dataArray.length; i++){
				strBuffer.append(dataArray[i]);
				if(i == 3 || i == 5){
					strBuffer.append("-");
				}
			}
			
			if(ToolUtil.isNotEmpty(excelItem.getGoodsbatchs())) {
				batchnumber = excelItem.getGoodsbatchs();
			}
			
			String recommend = getDatesssss();//入库批次
			if (ToolUtil.isNotEmpty(excelItem.getRecommend())) {
				char[] dataArray2 = excelItem.getRecommend().toCharArray();
				StringBuffer strBuffer2 = new StringBuffer();
				for(int i = 0; i < dataArray2.length; i++){
					strBuffer2.append(dataArray2[i]);
					if(i == 3 || i == 5){
						strBuffer2.append("-");
					}
				}
				recommend = strBuffer2.toString();
			}
			//创建订单明细
			OrderPutDetailedParam opdpDetailedParam = new OrderPutDetailedParam();
			opdpDetailedParam.setPutDetailedNumber(oprOrderPutResult.getPutNumber());//入库订单编号
			opdpDetailedParam.setDetailedGoodsname(bgrBasicGoodsResult.getGoodsName());//商品名称
			opdpDetailedParam.setDetailedGoodsnum(Long.parseLong(bgrBasicGoodsResult.getId().toString()));//商品id
			opdpDetailedParam.setGoodsNumber(bgrBasicGoodsResult.getGoodsNumber());//商品编号
			opdpDetailedParam.setGoodsNo(bgrBasicGoodsResult.getGoodsNo());//货号
			opdpDetailedParam.setDetailedPutbalenum(putnum.toString());//应入大包数
			opdpDetailedParam.setDetailedPutnum(putnum.toString());//应入数量
			opdpDetailedParam.setDetailedPutweight(putnum.multiply(goodsWeight).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//应入重量
			opdpDetailedParam.setGoodsWeight(goodsWeight.toString());//重量
			opdpDetailedParam.setDetailedPutvolume(putnum.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//应入体积
			opdpDetailedParam.setGoodsVolume(v.toString());//体积
			opdpDetailedParam.setDetailedUnitprice(u.toString());//单价
			opdpDetailedParam.setDetailedMoney(putnum.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//金额
			opdpDetailedParam.setDetailedBatchnumber(batchnumber);//批次
			opdpDetailedParam.setDetailedDateofmanufacture(strBuffer.toString());//生产日期
			opdpDetailedParam.setRecommend(recommend);//推荐批次
			opdpDetailedParam.setDetailedRebubbles(bgrBasicGoodsResult.getGoodsHeavybubble());//重泡货
			opdpDetailedParam.setDetailedLargeclass(bgrBasicGoodsResult.getGoodsBigtype());//大类 (导入品类描述)
			opdpDetailedParam.setDetailedGoodstype(bgrBasicGoodsResult.getGoodsType());//类别
			if(ToolUtil.isNotEmpty(excelItem.getGoodsUnit())) {
				opdpDetailedParam.setGoodsUnit(excelItem.getGoodsUnit());
			} else {
				opdpDetailedParam.setGoodsUnit(bgrBasicGoodsResult.getGoodsUnit());//单位
			}
			opdpDetailedParam.setDetailedPacking(bgrBasicGoodsResult.getGoodsPacking());//包装
			opdpDetailedParam.setGoodsSpecs(bgrBasicGoodsResult.getGoodsSpecs());//规格
			opdpDetailedParam.setCreateTime(new Date());
			detailedList.add(opdpDetailedParam);
			opdpDetailedParam = new OrderPutDetailedParam();
		}
		if(detailedList.size()>0){
			this.saveBatchList(orderPutList);
			orderPutDetailedService.saveBatchList(detailedList);
		}
		data.setSuccess(true);
		if (newgoodsflag) {
			data.setCode(450);
			data.setMessage("New product code: "+goodsnumbers+" Please maintain and correct the basic product information before submitting the warehousing order!");
		}
		return data;
	}

    @Resource
    private StorageStockDetailedService storageStockDetailedService;

	/**
	 * 撤回入库订单
	 */
	@Override
	@Transactional(rollbackFor = Exception.class,timeout=80)
	public ResponseData revokeS2(OrderPut orderPut, ResponseData data,User user) {
		//删除入库订单
		OrderPutParam orderPutParam = new OrderPutParam();
        ToolUtil.copyProperties(orderPut,orderPutParam);
        this.delete(orderPutParam);
        
		//删除入库订单明细
        OrderPutDetailedParam orderPutDetailedParam = new OrderPutDetailedParam();
        orderPutDetailedParam.setPutDetailedNumber(orderPutParam.getPutNumber());
        List<OrderPutDetailedResult> listde = orderPutDetailedService.findListBySpec(orderPutDetailedParam);
        for (OrderPutDetailedResult r : listde) {
        	orderPutDetailedParam.setId(r.getId());
        	orderPutDetailedService.delete(orderPutDetailedParam);
		}
        
        //查询入库单
        StoragePutParam param = new StoragePutParam();
        param.setPutNumber(orderPut.getPutNumber());
        param.setBillstate("revoke");
        StoragePutResult putResult = storagePutService.findBySpec(param);
        
        //删除入库单
        StoragePutParam param3 = new StoragePutParam();
        ToolUtil.copyProperties(putResult,param3);
        storagePutService.delete(param3);
        
        //查询入库单明细
        StoragePutSingleParam param2 = new StoragePutSingleParam();
        param2.setStorageNumber(putResult.getStorageNumber());
        List<StoragePutSingleResult> list = storagePutSingleService.findListBySpec(param2);
        
        //修改货运单
        OrderTransportParam orderTransportParam = new OrderTransportParam();
        orderTransportParam.setCustomerOrder(orderPut.getPutCustomerNumber());
        OrderTransportResult orderTransportResult = orderTransportService.findBySpec(orderTransportParam,null);
        if (orderTransportResult!=null) {
            ToolUtil.copyProperties(orderTransportResult,orderTransportParam);
            orderTransportParam.setTstate("h");
	        orderTransportParam.setTooVolume("0");
	        orderTransportParam.setTooNum("0");
	        orderTransportParam.setStockupareaId(null);
	        orderTransportParam.setAreanumber(null);
            orderTransportService.update(orderTransportParam);

            //修改调度单
            TDispatchParam dispatchParam2 = new TDispatchParam();
            dispatchParam2.setTransportNumber(orderTransportResult.getTransportNumber());
            List<TDispatchResult> list3 = tDispatchService.findListBySpec(dispatchParam2);
            for (TDispatchResult dispatchResult : list3) {
    			String transportNumber = dispatchResult.getTransportNumber().replaceAll(orderTransportResult.getTransportNumber()+";", "");
    			String customerOrder = dispatchResult.getCustomerOrder().replaceAll(orderTransportResult.getCustomerOrder()+";", "");
    			if (ToolUtil.isEmpty(transportNumber)) {
    		        ToolUtil.copyProperties(dispatchResult,dispatchParam2);
    				tDispatchService.delete(dispatchParam2);

					//删除回传单明细
					TransportReceiptParam param7 = new TransportReceiptParam();
					param7.setDispatchNumber(dispatchResult.getDispatchNumber());
					transportReceiptService.deleteBySpec(param7);
    			} else {
    				//判断是否是分配多个货运订单
    	    		TransportDispatchParam param9 = new TransportDispatchParam();
    	    		param9.setDispatchNumber(dispatchResult.getDispatchNumber());
    	    		List<TransportDispatchResult> list4 = transportDispatchService.sumBySpec(param9,orderTransportResult.getGetUnitId(),orderTransportResult.getGetLocationId());

					TransportReceiptParam param8 = new TransportReceiptParam();
					param8.setDispatchNumber(dispatchResult.getDispatchNumber());
					param8.setGetLocationid(orderTransportResult.getGetLocationId());
					param8.setGetUnitid(orderTransportResult.getGetUnitId());
					TransportReceiptResult receiptResult8 = transportReceiptService.findBySpec(param8);
    	    		if (list4.size()==1) {
    	    			//如果该地址下只有 一个单子
    					//重新合计 调度单数量体积
    					BigDecimal tnum = new BigDecimal(dispatchResult.getTooNum());
    					BigDecimal tv = new BigDecimal(dispatchResult.getToovolume());
    					BigDecimal rnum = new BigDecimal(receiptResult8.getNum());
    					BigDecimal rv = new BigDecimal(receiptResult8.getVolume());
    					
    					dispatchResult.setTooNum(tnum.subtract(rnum).toString());
    					dispatchResult.setToovolume(tv.subtract(rv).toString());
        				dispatchResult.setCustomerOrder(customerOrder);
        				dispatchResult.setTransportNumber(transportNumber);
        		        ToolUtil.copyProperties(dispatchResult,dispatchParam2);
        		        tDispatchService.update(dispatchParam2);

    					//删除回传单明细
    					transportReceiptService.deleteBySpec(param8);
					} else if (list4.size()>1) {
						//如果该地址下有多个单子
						for (TransportDispatchResult dispatchResult2 : list4 ) {
							if (dispatchResult2.getTransportNumber().equals(orderTransportResult.getTransportNumber())) {
		    					//重新合计 调度单数量体积,回传单数量体积
								BigDecimal tooDeployVolume = new BigDecimal(dispatchResult2.getTooDeployVolume());
								BigDecimal tooDeployNum = new BigDecimal(dispatchResult2.getTooDeployNum());

		    					BigDecimal rnum = new BigDecimal(receiptResult8.getNum());
		    					BigDecimal rv = new BigDecimal(receiptResult8.getVolume());
		    					receiptResult8.setNum(rnum.subtract(tooDeployNum).toString());
		    					receiptResult8.setVolume(rv.subtract(tooDeployVolume).toString());
		        		        ToolUtil.copyProperties(receiptResult8,param8);
		        		        transportReceiptService.update(param8);
		    					
		    					dispatchResult.setTooNum(tooDeployVolume.subtract(rnum).toString());
		    					dispatchResult.setToovolume(tooDeployNum.subtract(rv).toString());
		        				dispatchResult.setCustomerOrder(customerOrder);
		        				dispatchResult.setTransportNumber(transportNumber);
		        		        ToolUtil.copyProperties(dispatchResult,dispatchParam2);
		        		        tDispatchService.update(dispatchParam2);
							}
						}
					}
    			}
    		}
            
            //删除货运明细
    		OrderTransportDetailedParam detailedParam2 = new OrderTransportDetailedParam();
    		detailedParam2.setTransportNumber(orderTransportResult.getTransportNumber());
    		List<OrderTransportDetailedResult> mxlist = orderTransportDetailedService.findListBySpec(detailedParam2);
    		for (OrderTransportDetailedResult detailedr : mxlist) {
    	        ToolUtil.copyProperties(detailedr,detailedParam2);
    	        orderTransportDetailedService.delete(detailedParam2);
    		}
    		
    		//删除调度明细
    		TransportDispatchParam param4 = new TransportDispatchParam();
    		param4.setTransportNumber(orderTransportResult.getTransportNumber());
    		List<TransportDispatchResult> list2 = transportDispatchService.findListBySpec(param4);
    		Long deliverId = user.getDeliverId();
    		Long deptId = user.getDeptId();
    		for (TransportDispatchResult dispatchResult : list2) {
    	        ToolUtil.copyProperties(dispatchResult,param4);
    	        transportDispatchService.delete(param4);

		        //查询车辆
	    		BasicPrivatecarParam bpp = new BasicPrivatecarParam();
	    		bpp.setPrivatecarCarnumber(dispatchResult.getCarnumbger());
	    		bpp.setDeliverId(deliverId);
	    		BasicPrivatecarResult bpr = basicPrivatecarService.findBySpec(bpp,deptId,user.getOperateId());//车辆
	    		
				BigDecimal tooDeployVolume = new BigDecimal(dispatchResult.getTooDeployVolume());//配置体积
    	        
	    		//修改车辆  已配置体积
	    		BigDecimal toodeployvolume = new BigDecimal(bpr.getTooDeployvolume());//已配置体积
	    		BigDecimal ntoodeployvolume = toodeployvolume.subtract(tooDeployVolume);//已配置体积 - 新配体积 = 新车辆已配体积
	    		BigDecimal as = new BigDecimal(0);
	    		if (ntoodeployvolume.compareTo(as)==0) {
					bpr.setPrivatecarState("IDLE");
				}
				bpr.setTooDeployvolume(ntoodeployvolume.toString());
				ToolUtil.copyProperties(bpr,bpp);
				basicPrivatecarService.update(bpp);
			}
		}
        
        for (StoragePutSingleResult putSingleResult : list) {
			if ("NOT_STARTED".equals(putSingleResult.getSingleState())) {
		        ToolUtil.copyProperties(putSingleResult,param2);
		        storagePutSingleService.delete(param2);//删除入库单明细
				try {
					BasicTrayParam basicTrayparam = new BasicTrayParam();
					basicTrayparam.setTrayNumber(param2.getTrayNumber());
					basicTrayparam.setDeptId(user.getDeptId());
					basicTrayparam.setDeliverId(user.getDeliverId());
					BasicTrayResult basicTrayResult = basicTrayService.findBySpec(basicTrayparam,user.getOperateId());
					if (basicTrayResult != null) {
						BasicTrayParam basicTrayParam = new BasicTrayParam();
						basicTrayParam.setId(basicTrayResult.getId());
						basicTrayParam.setTrayState("IDLE");
						basicTrayService.update(basicTrayParam);
					}
				} catch (Exception e) {
				}
			} else if ("NOT_RECEIVED".equals(putSingleResult.getSingleState())) {
		        ToolUtil.copyProperties(putSingleResult,param2);
		        storagePutSingleService.delete(param2);//删除入库单明细
				try {
					BasicTrayParam basicTrayparam = new BasicTrayParam();
					basicTrayparam.setTrayNumber(param2.getTrayNumber());
					basicTrayparam.setDeptId(user.getDeptId());
					basicTrayparam.setDeliverId(user.getDeliverId());
					BasicTrayResult basicTrayResult = basicTrayService.findBySpec(basicTrayparam,user.getOperateId());
					if (basicTrayResult != null) {
						BasicTrayParam basicTrayParam = new BasicTrayParam();
						basicTrayParam.setId(basicTrayResult.getId());
						basicTrayParam.setTrayState("IDLE");
						basicTrayService.update(basicTrayParam);
					}
				} catch (Exception e) {
				}
		        
	            StorageStockParam ssp = new StorageStockParam();
	            ssp.setStockstate("NOT_RECEIVED");
                ssp.setTrayNumber(putSingleResult.getTrayNumber());
                ssp.setGoodsId(putSingleResult.getGoodsId());
                StorageStockResult ssr = storageStockService.findBySpec(ssp);
                if (ssr!=null) {
                	ToolUtil.copyProperties(ssr,ssp);
                	storageStockService.delete(ssp);//删除未入库存
				}
			} else if ("RECEIVED".equals(putSingleResult.getSingleState())) {
				BigDecimal realNum = new BigDecimal(putSingleResult.getRealNum());//入库实际数量

	            StorageStockParam ssp = new StorageStockParam();
                ssp.setTrayNumber(putSingleResult.getTrayNumber());
                ssp.setGoodsId(putSingleResult.getGoodsId());
                ssp.setGoodsbatchs(putSingleResult.getSingleBatchnumber());
                StorageStockResult ssr = storageStockService.findBySpec(ssp);
                if (ssr==null) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
    				data.setSuccess(false);
    				data.setMessage("Inventory changed!");
    				return data;
				}
                BigDecimal num = new BigDecimal(ssr.getNum());//库存数
                if ("IN_STOCK".equals(ssr.getStockstate())) {
                    if (realNum.compareTo(num)!=0) {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
        				data.setSuccess(false);
        				data.setMessage("Inventory changed!");
        				return data;
    				} else {
    			        ToolUtil.copyProperties(ssr,ssp);
		                storageStockDetailedService.addNewDetailed("TRANSFER_IN", "", "REVOKE_TRANSFER", "", ssp,user);
    			        storageStockService.delete(ssp);//删除库存
    					try {
    						BasicTrayParam basicTrayparam = new BasicTrayParam();
    						basicTrayparam.setTrayNumber(param2.getTrayNumber());
    						basicTrayparam.setDeptId(user.getDeptId());
    						basicTrayparam.setDeliverId(user.getDeliverId());
    						BasicTrayResult basicTrayResult = basicTrayService.findBySpec(basicTrayparam,user.getOperateId());
    						if (basicTrayResult != null) {
    							BasicTrayParam basicTrayParam = new BasicTrayParam();
    							basicTrayParam.setId(basicTrayResult.getId());
    							basicTrayParam.setTrayState("IDLE");
    							basicTrayService.update(basicTrayParam);
    						}
    					} catch (Exception e) {
    					}
    				}
				} else {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
    				data.setSuccess(false);
    				data.setMessage("Inventory changed!");
    				return data;
				}
		        ToolUtil.copyProperties(putSingleResult,param2);
		        storagePutSingleService.delete(param2);//删除入库单明细
			} else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
				data.setSuccess(false);
				return data;
			}
		}
		data.setSuccess(true);
		return data;
	}
	
	/**
	 * 撤回入库订单
	 */
	@Override
	@Transactional(rollbackFor = Exception.class,timeout=400)
	public ResponseData revokeS(OrderPut orderPut, ResponseData data) {
        BasicCustomer c = basicCustomerService.getById(orderPut.getPutCustomerId());
		User user = userService.getcurrentUser();
		//修改入库订单状态
		OrderPutParam orderPutParam = new OrderPutParam();
        ToolUtil.copyProperties(orderPut,orderPutParam);
        orderPutParam.setBillstate("REVOKED");
        this.update(orderPutParam);
        
        //查询入库单
        StoragePutParam param = new StoragePutParam();
        param.setPutNumber(orderPut.getPutNumber());
        param.setBillstate("revoke");
        StoragePutResult putResult = storagePutService.findBySpec(param);
        
        //修改入库单
        int storageNumber = 1;
        StoragePutParam param3 = new StoragePutParam();
        param3.setStorageNumber("CH"+putResult.getStorageNumber()+"0"+storageNumber);
        StoragePutResult putResult2 = storagePutService.findBySpec(param3);
        while (putResult2!=null) {
        	storageNumber++;
            param3.setStorageNumber("CH"+putResult.getStorageNumber()+"0"+storageNumber);
            putResult2 = storagePutService.findBySpec(param3);
		}
        ToolUtil.copyProperties(putResult,param3);
        param3.setBillstate("REVOKED");
        param3.setStorageNumber("CH"+putResult.getStorageNumber()+"0"+storageNumber);
        storagePutService.update(param3);
        
        //查询入库单明细
        StoragePutSingleParam param2 = new StoragePutSingleParam();
        param2.setStorageNumber(putResult.getStorageNumber());
        List<StoragePutSingleResult> list = storagePutSingleService.findListBySpec(param2);

        if(c != null && ToolUtil.isNotEmpty(c.getWmsAddtms())&&c.getWmsAddtms().equals("1")) {
        	//修改货运单
        	OrderTransportParam orderTransportParam = new OrderTransportParam();
        	orderTransportParam.setCustomerOrder(orderPut.getPutCustomerNumber());
        	OrderTransportResult orderTransportResult = orderTransportService.findBySpec(orderTransportParam,null);
        	if (orderTransportResult!=null) {
        		//删除调度明细
        		TransportDispatchParam param4 = new TransportDispatchParam();
        		param4.setTransportNumber(orderTransportResult.getTransportNumber());
        		List<TransportDispatchResult> list2 = transportDispatchService.findListBySpec(param4);
        		TransportDispatchParam transportDispatchParam = new TransportDispatchParam();
        		for (TransportDispatchResult dispatchResult : list2) {
        			if(ToolUtil.isNotEmpty(dispatchResult.getDispatchNumber())) {
        				transportDispatchParam.setId(dispatchResult.getId());
        				data = this.transportDispatchService.removeGoodsDispatch(transportDispatchParam ,data,user);
        				if(!data.getSuccess()) {
        					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
        					return data;
        				}
        			} else {
        				data = this.transportDispatchService.removeGoodsWeb(dispatchResult.getTransportNumber(),dispatchResult.getGoodsId(),dispatchResult.getCarnumbger(),data,user);
        				if(!data.getSuccess()) {
        					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
        					return data;
        				}
        			}
        			ToolUtil.copyProperties(dispatchResult,param4);
        			transportDispatchService.delete(param4);
        		}
        		
        		//删除货运明细
        		OrderTransportDetailedParam detailedParam2 = new OrderTransportDetailedParam();
        		detailedParam2.setTransportNumber(orderTransportResult.getTransportNumber());
        		List<OrderTransportDetailedResult> mxlist = orderTransportDetailedService.findListBySpec(detailedParam2);
        		for (OrderTransportDetailedResult detailedr : mxlist) {
        			ToolUtil.copyProperties(detailedr,detailedParam2);
        			orderTransportDetailedService.delete(detailedParam2);
        		}
        		
        		ToolUtil.copyProperties(orderTransportResult,orderTransportParam);
        		orderTransportParam.setTstate("h");
        		orderTransportParam.setTooVolume("0");
        		orderTransportParam.setTooNum("0");
        		orderTransportParam.setStockupareaId(null);
        		orderTransportParam.setAreanumber(null);
        		orderTransportService.update(orderTransportParam);
        	}
        }
        
        boolean delete = true;
        for (StoragePutSingleResult putSingleResult : list) {
			if ("PENDING_APPROVAL".equals(putSingleResult.getSingleState())) {
		        ToolUtil.copyProperties(putSingleResult,param2);
		        storagePutSingleService.delete(param2);//删除入库单明细
				try {
					BasicTrayParam basicTrayparam = new BasicTrayParam();
					basicTrayparam.setTrayNumber(param2.getTrayNumber());
					basicTrayparam.setDeptId(userService.getDeptId());
					basicTrayparam.setDeliverId(userService.getDeliverId());
					BasicTrayResult basicTrayResult = basicTrayService.findBySpec(basicTrayparam,user.getOperateId());
					if (basicTrayResult != null) {
						BasicTrayParam basicTrayParam = new BasicTrayParam();
						basicTrayParam.setId(basicTrayResult.getId());
						basicTrayParam.setTrayState("IDLE");
						basicTrayService.update(basicTrayParam);
					}
				} catch (Exception e) {
				}
				StorageStockParam paraStockParam = new StorageStockParam();
				paraStockParam.setId(Integer.parseInt(putSingleResult.getStockId().toString()));
				storageStockService.delete(paraStockParam);
			} else if ("NOT_RECEIVED".equals(putSingleResult.getSingleState())) {
		        ToolUtil.copyProperties(putSingleResult,param2);
		        storagePutSingleService.delete(param2);//删除入库单明细
				try {
					BasicTrayParam basicTrayparam = new BasicTrayParam();
					basicTrayparam.setTrayNumber(param2.getTrayNumber());
					basicTrayparam.setDeptId(user.getDeptId());
					basicTrayparam.setDeliverId(user.getDeliverId());
					BasicTrayResult basicTrayResult = basicTrayService.findBySpec(basicTrayparam,user.getOperateId());
					if (basicTrayResult != null) {
						BasicTrayParam basicTrayParam = new BasicTrayParam();
						basicTrayParam.setId(basicTrayResult.getId());
						basicTrayParam.setTrayState("IDLE");
						basicTrayService.update(basicTrayParam);
					}
				} catch (Exception e) {
				}
		        
				if(putSingleResult.getStockId() != null) {
					StorageStockParam paraStockParam = new StorageStockParam();
					paraStockParam.setId(Integer.parseInt(putSingleResult.getStockId().toString()));
					storageStockService.delete(paraStockParam);
				}
			} else if ("RECEIVED".equals(putSingleResult.getSingleState())) {
				BigDecimal realNum = new BigDecimal(putSingleResult.getRealNum());//入库实际数量

	            StorageStockParam ssp = new StorageStockParam();
                StorageStock ssr = storageStockService.getById(putSingleResult.getStockId());
                if (ssr==null) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
    				data.setSuccess(false);
    				data.setMessage("Inventory changed!");
    				return data;
				}
                BigDecimal num = new BigDecimal(ssr.getNum());//库存数
                if ("IN_STOCK".equals(ssr.getStockstate())) {
                    if (realNum.compareTo(num)!=0) {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
        				data.setSuccess(false);
        				data.setMessage("Inventory changed!");
        				return data;
    				} else {
    			        ToolUtil.copyProperties(ssr,ssp);
    			        storageStockDetailedService.addNewDetailed("INBOUND",putSingleResult.getStorageNumber(),"INBOUND_REVOKED","",ssp,user);
    			        storageStockService.delete(ssp);//删除库存
    					try {
    						BasicTrayParam basicTrayparam = new BasicTrayParam();
    						basicTrayparam.setTrayNumber(putSingleResult.getTrayNumber());
    						basicTrayparam.setDeptId(user.getDeptId());
    						basicTrayparam.setDeliverId(user.getDeliverId());
    						BasicTrayResult basicTrayResult = basicTrayService.findBySpec(basicTrayparam,user.getOperateId());
    						if (basicTrayResult != null) {
    							BasicTrayParam basicTrayParam = new BasicTrayParam();
    							basicTrayParam.setId(basicTrayResult.getId());
    							basicTrayParam.setTrayState("IDLE");
    							basicTrayService.update(basicTrayParam);
    						}
    					} catch (Exception e) {
    					}
    				}
				} else if ("NOT_RECEIVED".equals(ssr.getStockstate())){
					ssp.setId(ssr.getId());
			        storageStockService.delete(ssp);//删除库存
					try {
						BasicTrayParam basicTrayparam = new BasicTrayParam();
						basicTrayparam.setTrayNumber(putSingleResult.getTrayNumber());
						basicTrayparam.setDeptId(user.getDeptId());
						basicTrayparam.setDeliverId(user.getDeliverId());
						BasicTrayResult basicTrayResult = basicTrayService.findBySpec(basicTrayparam,user.getOperateId());
						if (basicTrayResult != null) {
							BasicTrayParam basicTrayParam = new BasicTrayParam();
							basicTrayParam.setId(basicTrayResult.getId());
							basicTrayParam.setTrayState("IDLE");
							basicTrayService.update(basicTrayParam);
						}
					} catch (Exception e) {
					}
					
				} else {
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
					data.setSuccess(false);
					data.setMessage("Inventory changed!");
					return data;
				}
		        ToolUtil.copyProperties(putSingleResult,param2);
		        param2.setSingleState("REVOKED");
		        param2.setStorageNumber("CH"+putResult.getStorageNumber()+"0"+storageNumber);
		        storagePutSingleService.update(param2);//修改入库单明细
		        delete = false;
			} else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
				data.setSuccess(false);
				return data;
			}
		}
        if (delete) {
            storagePutService.delete(param3);
		}
		data.setSuccess(true);
		return data;
	}

	/**
	 * 撤回
	 * 直接入库的单据
	 */
	@Override
	@Transactional(rollbackFor = Exception.class,timeout=80)
	public ResponseData revokeStrides(OrderPut orderPut, ResponseData data) {
		//修改入库订单状态
		OrderPutParam orderPutParam = new OrderPutParam();
        ToolUtil.copyProperties(orderPut,orderPutParam);
        orderPutParam.setBillstate("REVOKED");
        this.update(orderPutParam);
		
        OrderPutDetailedParam orderPutDetailedParam = new OrderPutDetailedParam();
        orderPutDetailedParam.setPutDetailedNumber(orderPut.getPutNumber());
        List<OrderPutDetailedResult> dlist = orderPutDetailedService.findListBySpec(orderPutDetailedParam);
        for (OrderPutDetailedResult r : dlist) {
        	StorageStockParam storageStockParam = new StorageStockParam();
        	storageStockParam.setGoodsId(r.getDetailedGoodsnum());
        	storageStockParam.setGoodsbatchs(r.getDetailedBatchnumber());
        	storageStockParam.setWarehouseId(orderPut.getWarehouseId());
        	List<StorageStockResult> slist = storageStockService.findListBySpec(storageStockParam,null);
        	if(slist.size()==0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
        		return ResponseData.error("Inventory changed!");
        	}
			BasicGoods bG = basicGoodsService.getById(r.getDetailedGoodsnum());//商品
			BigDecimal detailedPutnum = new BigDecimal(r.getDetailedPutnum());//入库数量
			BigDecimal w = new BigDecimal(bG.getGoodsWeight());//重量
			BigDecimal v = new BigDecimal(bG.getGoodsVolume());//体积
			BigDecimal u = new BigDecimal(bG.getGoodsCharging());//单价compareTo
			BigDecimal as = new BigDecimal(0);
        	for (StorageStockResult sr : slist) {
    			BigDecimal num = new BigDecimal(sr.getNum());//库存数
        		if(num.compareTo(detailedPutnum) == 1) {
        			num = num.subtract(detailedPutnum).setScale(3, BigDecimal.ROUND_HALF_DOWN);
        			StorageStockParam storageStockUpdate = new StorageStockParam();
        			storageStockUpdate.setId(sr.getId());
        			storageStockUpdate.setNum(num.toString());
        			storageStockUpdate.setBalenum(num.toString());
        			storageStockUpdate.setCanNum(num.toString());
        			storageStockUpdate.setStockWeight(w.multiply(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
        			storageStockUpdate.setStockVolume(v.multiply(num).setScale(4, BigDecimal.ROUND_HALF_DOWN).toString());
        			storageStockUpdate.setUnitprice(u.toString());
        			storageStockUpdate.setStockMoney(u.multiply(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
        			storageStockService.update(storageStockUpdate);
        			detailedPutnum = as;
        			break;
        		}
        		if(num.compareTo(detailedPutnum) == 0) {
        			StorageStockParam storageStockUpdate = new StorageStockParam();
        			storageStockUpdate.setId(sr.getId());
        			storageStockService.delete(storageStockUpdate);
        			detailedPutnum = as;
        			break;
        		}
        		if(num.compareTo(detailedPutnum) == -1) {
        			StorageStockParam storageStockUpdate = new StorageStockParam();
        			storageStockUpdate.setId(sr.getId());
        			storageStockService.delete(storageStockUpdate);
        			detailedPutnum = detailedPutnum.subtract(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);
        		}
			}
        	if(detailedPutnum.compareTo(as) == 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
        		return ResponseData.error("Inventory changed!");
        	}
		}
		return ResponseData.success();
	}

	@Override
	public OrderPutResult getPrintOrderPut(OrderPutParam orderPutParam) {
		return this.baseMapper.getPrintOrderPut(orderPutParam);
	}

	/**
	 * 保存聚水潭采购入库单
	 */
	@Override
	public ResponseData savePulljstputorder(User user,OrderPutParam orderPutParam, List<OrderPutExcelItem> list) {
		ResponseData data = new ResponseData();
		if(orderPutParam.getWarehouseId()==null||orderPutParam.getPutCustomerId()==null) {
			return ResponseData.error("Please fill in the order basic information!");
		}
		List<OrderPutParam> orderPutList = new ArrayList<OrderPutParam>();//批量保存的 入库订单list
		List<OrderPutDetailedParam> detailedList = new ArrayList<OrderPutDetailedParam>();//批量保存的 订单商品明细list
		BasicCustomer basicCustomer = basicCustomerService.getById(orderPutParam.getPutCustomerId());//客户信息
		BasicWarehouse basicWarehouse = basicWarehouseService.getById(orderPutParam.getWarehouseId());//仓库信息
		//查询最新 入库订单
    	OrderPut orderPut = this.findByThelastone();
    	int putNumber = 1;
    	if (orderPut!=null) {
    		putNumber = orderPut.getId()+1;
		} 
    	boolean newgoodsflag = false;
    	String goodsnumbers = "";
//    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
		String trayCode = getOperateIdOrderPrefix(user);
		for (OrderPutExcelItem excelItem : list) {
			if (ToolUtil.isEmpty(excelItem.getGoodsNumber())&&ToolUtil.isEmpty(excelItem.getGoodsName())) {
				continue;
			} 
			if(ToolUtil.isEmpty(excelItem.getPutnum())) {
				continue;
			}
			//通过 导入的客户单号 查询是否存在
			OrderPutParam opp = new OrderPutParam();
			boolean whether2 = false;
			if(ToolUtil.isEmpty(excelItem.getShipmentnumber())) {
				whether2 = true;
				excelItem.setShipmentnumber(trayCode+"-"+"RK-"+cn.stylefeng.guns.core.util.DateUtil.getDateBch()+"-"+(putNumber));
			}
			opp.setPutCustomerNumber(excelItem.getShipmentnumber());//客户单号 (客户货运订单号)
			OrderPutResult oprOrderPutResult = this.findBySpec(opp,null);//状态不等于 作废



			//如果不存在
			if(oprOrderPutResult==null) {
				//新增入库订单
				opp.setPutNumber(trayCode+"-"+"RK-"+cn.stylefeng.guns.core.util.DateUtil.getDateBch()+"-"+(putNumber));
		    	opp.setPutCustomerId(orderPutParam.getPutCustomerId());//客户id
		    	opp.setPutCustomerName(basicCustomer.getCustomerName());//客户名称
		    	opp.setSettlementunit(basicCustomer.getCustomerSettlement());//结算单位
		    	opp.setWarehouseId(orderPutParam.getWarehouseId());//仓库id
		    	opp.setWarehouseName(basicWarehouse.getWarehouseName());//仓库名称
		    	opp.setPutType("PICKUP_STORAGE");//入库类别 默认为‘提货入库’
	opp.setPutMode("WINDOW_RECEIVING");//入库方式 默认为‘窗口接货’  不生成货运订单
		    	opp.setSettlement(orderPutParam.getSettlement());//结算方式
		    	opp.setSettlementunit(null);
		    	opp.setCarnumber(excelItem.getCarNumber());//车牌号
		    	opp.setShipmentnumber(excelItem.getPutCustomerNumber());//客户交货单号
		    	opp.setBillstate("PENDING_SUBMIT");//订单状态
		    	opp.setCreateName(user.getName());//制单人名称
		    	opp.setCreateTime(new Date());
		    	opp.setCreateUser(user.getUserId());
		    	opp.setNotes(excelItem.getNotes());
		    	opp.setDeliverId(deliverId);//交付id
		    	opp.setDeptId(deptId);//部门id
		    	boolean whether = false;//判断客户单号  是否重复
		    	for (OrderPutParam opp2 : orderPutList) {
					if (opp2.getPutCustomerNumber().equals(opp.getPutCustomerNumber())) {
						whether = true;
				    	oprOrderPutResult = new OrderPutResult();
		    			ToolUtil.copyProperties(opp2,oprOrderPutResult);
					}
				}
		    	if (!whether) {
		    		orderPutList.add(opp);
		    		if(!whether2) {
		    			putNumber++;
		    		}
			    	oprOrderPutResult = new OrderPutResult();
	    			ToolUtil.copyProperties(opp,oprOrderPutResult);
				}
    			opp = new OrderPutParam();
			} else {
				//如果 导入的客户单号 已存在
				continue;
			}
			
			//通过商品编号 查询商品是否存在
			BasicGoodsParam bgpParam = new BasicGoodsParam();
			bgpParam.setGoodsNumber(excelItem.getGoodsNumber());
			bgpParam.setGoodsCustomerId(Integer.parseInt(orderPutParam.getPutCustomerId().toString()));
			BasicGoodsResult bgrBasicGoodsResult = basicGoodsService.findBySpec(bgpParam,null);
			String sku_code = "";
			if(bgrBasicGoodsResult != null){
				 sku_code = bgrBasicGoodsResult.getGoodsNo();
			}

			//如果商品不存在  或者商品69码不存在
			if(bgrBasicGoodsResult == null ||ToolUtil.isEmpty(bgrBasicGoodsResult.getGoodsNo())){
				try {
					//聚水潭商品编码获取国标码
					String  goodsInfo = justGoodsInfoApi.getSellerItem(basicCustomer.getJustAppkey(),
							basicCustomer.getJustAppsecret(),
							basicCustomer.getAccessToken(),
							excelItem.getGoodsNumber()+",",
							"1"
					);
					org.json.JSONObject jsonObject = new org.json.JSONObject(goodsInfo);
					if(jsonObject.get("code").toString().equals("0")) {
						String data1 = jsonObject.get("data").toString();
						JSONObject jsonObject1 = new JSONObject(data1);
						if (Integer.parseInt(jsonObject1.get("data_count").toString()) > 0) {

							Object datas = jsonObject1.get("datas");
							JSONArray jsonArray = new JSONArray(datas);
							Object o = jsonArray.get(0);
							JSONObject jsonObject2 = new JSONObject(o);
							sku_code = jsonObject2.get("sku_code").toString();
							bgpParam.setGoodsNo(sku_code);
						}
					}

				} catch (IOException e) {
					e.printStackTrace();
				}
			}


			//计算 单价，体积单位
			BigDecimal putnum = new BigDecimal(excelItem.getPutnum()).setScale(3, BigDecimal.ROUND_HALF_DOWN);//应入数量
			BigDecimal putvolume =  new BigDecimal(0);//应入体积
			BigDecimal v = new BigDecimal(0);//体积单位
			BigDecimal u = new BigDecimal(0);//单价
			if (ToolUtil.isNotEmpty(excelItem.getPutvolume())) {
				putvolume =  new BigDecimal(excelItem.getPutvolume()).setScale(3, BigDecimal.ROUND_HALF_DOWN);
				v = putvolume.divide(putnum,8,BigDecimal.ROUND_HALF_DOWN);//(四舍五入保留8位)
			} else {
				if (bgrBasicGoodsResult != null && ToolUtil.isNotEmpty(bgrBasicGoodsResult.getGoodsVolume())) {
					v = new BigDecimal(bgrBasicGoodsResult.getGoodsVolume()).setScale(8, BigDecimal.ROUND_HALF_DOWN);//(四舍五入保留8位)
					putvolume =  putnum.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN);
				} else {
					v =  new BigDecimal("0.1");
				}
			}
			BigDecimal detailedMoney = new BigDecimal(0);//金额
			if (ToolUtil.isNotEmpty(excelItem.getDetailedMoney())) {
				detailedMoney = new BigDecimal(excelItem.getDetailedMoney()).setScale(3, BigDecimal.ROUND_HALF_DOWN);
				u = detailedMoney.divide(putnum,3,BigDecimal.ROUND_HALF_DOWN);//(四舍五入保留3位)
			} else {
				if (bgrBasicGoodsResult != null && ToolUtil.isNotEmpty(bgrBasicGoodsResult.getGoodsCharging())) {
					u = new BigDecimal(bgrBasicGoodsResult.getGoodsCharging()).setScale(3, BigDecimal.ROUND_HALF_DOWN);
					detailedMoney = putnum.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN);
				} else {
					u = new BigDecimal("10");
				}
			}

			BigDecimal as = new BigDecimal(0);
			//如果商品不存在  新增基础信息
			if(bgrBasicGoodsResult == null){
				bgpParam.setGoodsCustomerName(basicCustomer.getCustomerName());//客户名称
				bgpParam.setGoodsName(excelItem.getGoodsName());//商品名称
				bgpParam.setGoodsUnit(excelItem.getGoodsUnit());//单位 (基本单位)
				bgpParam.setGoodsBigtype(excelItem.getGoodsType());//商品大类 (品类描述)
				bgpParam.setGoodsWeight("0.1");//重量单位 默认1
				if (v.compareTo(as)==1) {//大于 返回1
					bgpParam.setGoodsVolume(v.toString());//体积
				} else {
					bgpParam.setGoodsVolume("0.1");//默认1
				}
				if(u.compareTo(as)==1) {//大于 返回1
					bgpParam.setGoodsCharging(u.toString());//计费指定价
				} else {
					bgpParam.setGoodsCharging("10");//默认1
				}
				bgpParam.setGoodsBottom("10");//底
				bgpParam.setGoodsLamination("10");//叠层
				bgpParam.setGoodsSupportvolume("10000");//每托存量 默认100
				bgpParam.setGoodsTraylayer("1");//托层 默认1
				bgpParam.setGoodsWarranty("36");
				bgpParam.setCreateTime(new Date());
				bgpParam.setDeliverId(deliverId);
				bgpParam.setDeptId(deptId);
				basicGoodsService.addReturnId(bgpParam);
				bgrBasicGoodsResult = new BasicGoodsResult();
				ToolUtil.copyProperties(bgpParam, bgrBasicGoodsResult);
				newgoodsflag = true;
				goodsnumbers += excelItem.getGoodsNumber()+";";
			} else {
				boolean flag = false;//是否更新商品
				if (ToolUtil.isNotEmpty(bgrBasicGoodsResult.getGoodsVolume())) {
					BigDecimal volume =  new BigDecimal(bgrBasicGoodsResult.getGoodsVolume());
					if(v.compareTo(volume) != 0) {
						bgrBasicGoodsResult.setGoodsVolume(v.toString());
						flag = true;
					}
				} else {
					bgrBasicGoodsResult.setGoodsVolume(v.toString());
					flag = true;
				}
				if (ToolUtil.isNotEmpty(bgrBasicGoodsResult.getGoodsCharging())) {
					BigDecimal charging =  new BigDecimal(bgrBasicGoodsResult.getGoodsCharging());
					if(u.compareTo(charging) != 0) {
						bgrBasicGoodsResult.setGoodsCharging(u.toString());
						flag = true;
					}
				} else {
					bgrBasicGoodsResult.setGoodsCharging(u.toString());
					flag = true;
				}
				if(ToolUtil.isNotEmpty(bgpParam.getGoodsNo()) && bgpParam.getGoodsNo().equals(bgrBasicGoodsResult.getGoodsNo())) {
					flag = true;
				}

				if (flag) {
					ToolUtil.copyProperties(bgrBasicGoodsResult, bgpParam);
					bgpParam.setGoodsNo(sku_code);
					basicGoodsService.update(bgpParam);
				}
			}
			BigDecimal goodsWeight = new BigDecimal(bgrBasicGoodsResult.getGoodsWeight());//重量单位
			String batchnumber = excelItem.getBatchnumber();//导入生产日期
			if(ToolUtil.isEmpty(batchnumber)){
				batchnumber = cn.stylefeng.guns.core.util.DateUtil.getDateBch();
			}
			char[] dataArray = batchnumber.toCharArray();
			StringBuffer strBuffer = new StringBuffer();
			if(batchnumber.length()==10) {
				batchnumber = "20"+batchnumber.substring(0,6);//恒安导入 2111120456
			} else  if(batchnumber.length()==19) {
				batchnumber = batchnumber.substring(0, 10).replace("-","");
			}else {
				batchnumber = batchnumber.substring(0, 8);//贝因美导入 20210613
			}
			dataArray = batchnumber.toCharArray();
			for(int i = 0; i < dataArray.length; i++){
				strBuffer.append(dataArray[i]);
				if(i == 3 || i == 5){
					strBuffer.append("-");
				}
			}
			
			if(ToolUtil.isNotEmpty(excelItem.getGoodsbatchs())) {
				batchnumber = excelItem.getGoodsbatchs();
			}
			
			String recommend = getDatesssss();//入库批次
			if (ToolUtil.isNotEmpty(excelItem.getRecommend())) {
				char[] dataArray2 = excelItem.getRecommend().toCharArray();
				StringBuffer strBuffer2 = new StringBuffer();
				for(int i = 0; i < dataArray2.length; i++){
					strBuffer2.append(dataArray2[i]);
					if(i == 3 || i == 5){
						strBuffer2.append("-");
					}
				}
				recommend = strBuffer2.toString();
			}
			//创建订单明细
			OrderPutDetailedParam opdpDetailedParam = new OrderPutDetailedParam();
			opdpDetailedParam.setPutDetailedNumber(oprOrderPutResult.getPutNumber());//入库订单编号
			opdpDetailedParam.setDetailedGoodsname(bgrBasicGoodsResult.getGoodsName());//商品名称
			opdpDetailedParam.setDetailedGoodsnum(Long.parseLong(bgrBasicGoodsResult.getId().toString()));//商品id
			opdpDetailedParam.setGoodsNumber(bgrBasicGoodsResult.getGoodsNumber());//商品编号
			opdpDetailedParam.setDetailedPutbalenum(putnum.toString());//应入大包数
			opdpDetailedParam.setDetailedPutnum(putnum.toString());//应入数量
			opdpDetailedParam.setDetailedPutweight(putnum.multiply(goodsWeight).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//应入重量
			opdpDetailedParam.setGoodsWeight(goodsWeight.toString());//重量
			opdpDetailedParam.setDetailedPutvolume(putnum.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//应入体积
			opdpDetailedParam.setGoodsVolume(v.toString());//体积
			opdpDetailedParam.setDetailedUnitprice(u.toString());//单价
			opdpDetailedParam.setDetailedMoney(putnum.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//金额
			opdpDetailedParam.setDetailedBatchnumber(batchnumber);//批次
			opdpDetailedParam.setDetailedDateofmanufacture(strBuffer.toString());//生产日期
			opdpDetailedParam.setRecommend(recommend);//推荐批次
			opdpDetailedParam.setDetailedRebubbles(bgrBasicGoodsResult.getGoodsHeavybubble());//重泡货
			opdpDetailedParam.setDetailedPacking(bgrBasicGoodsResult.getGoodsPacking());//包装
			opdpDetailedParam.setDetailedLargeclass(bgrBasicGoodsResult.getGoodsBigtype());//商品大类 (导入品类描述)
			opdpDetailedParam.setDetailedGoodstype(bgrBasicGoodsResult.getGoodsType());//商品类别
			opdpDetailedParam.setGoodsNo(bgrBasicGoodsResult.getGoodsNo());
			if(ToolUtil.isNotEmpty(excelItem.getGoodsUnit())) {
				opdpDetailedParam.setGoodsUnit(excelItem.getGoodsUnit());
			} else {
				opdpDetailedParam.setGoodsUnit(bgrBasicGoodsResult.getGoodsUnit());
			}
			opdpDetailedParam.setGoodsSpecs(bgrBasicGoodsResult.getGoodsSpecs());
			opdpDetailedParam.setCreateTime(new Date());
			detailedList.add(opdpDetailedParam);
			opdpDetailedParam = new OrderPutDetailedParam();
		}
		if(detailedList.size()>0){
			this.saveBatchList(orderPutList);
			orderPutDetailedService.saveBatchList(detailedList);
			Integer[] integers = orderPutList.stream().map(OrderPutParam::getId).toArray(Integer[]::new);
			long[] longs = Arrays.stream(integers).mapToLong(Long::valueOf).toArray();

			jstPutOrderScheduledService.submitJSTPutOrderNew(longs,user);
		}
		data.setSuccess(true);
		if (newgoodsflag) {
			data.setCode(450);
			data.setMessage("New product code: "+goodsnumbers+" Please maintain and correct the basic product information before submitting the warehousing order!");
		}
		return data;
	}

	/**
	 * 保存聚水潭退货入库明细
	 */
	@Override
	public ResponseData savePulljstreturngoods(OrderPutParam orderPutParam, List<OrderPutExcelItem> list) {
		ResponseData data = new ResponseData();
		if(orderPutParam.getWarehouseId()==null||orderPutParam.getPutCustomerId()==null) {
			return ResponseData.error("Please fill in the order basic information!");
		}
		List<OrderPutParam> orderPutList = new ArrayList<OrderPutParam>();//批量保存的 入库订单list
		List<OrderPutDetailedParam> detailedList = new ArrayList<OrderPutDetailedParam>();//批量保存的 订单商品明细list
		BasicCustomer basicCustomer = basicCustomerService.getById(orderPutParam.getPutCustomerId());//客户信息
		BasicWarehouse basicWarehouse = basicWarehouseService.getById(orderPutParam.getWarehouseId());//仓库信息
		//查询最新 入库订单
    	OrderPut orderPut = this.findByThelastone();
    	int putNumber = 1;
    	if (orderPut!=null) {
    		putNumber = orderPut.getId()+1;
		} 
    	boolean newgoodsflag = false;
    	String goodsnumbers = "";
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
		String trayCode = getOperateIdOrderPrefix(user);
		BigDecimal as = new BigDecimal(0);
		for (OrderPutExcelItem excelItem : list) {
			BigDecimal putnum = new BigDecimal(excelItem.getPutnum()).setScale(3, BigDecimal.ROUND_HALF_DOWN);//应入数量
			if(putnum.compareTo(as) != 1) {
				continue;
			}
			if (ToolUtil.isEmpty(excelItem.getGoodsNumber())&&ToolUtil.isEmpty(excelItem.getGoodsName())) {
				continue;
			} 
			if(ToolUtil.isEmpty(excelItem.getPutnum())) {
				continue;
			}
			//通过 导入的客户单号 查询是否存在
			OrderPutParam opp = new OrderPutParam();
			boolean whether2 = false;
			if(ToolUtil.isEmpty(excelItem.getShipmentnumber())) {
				whether2 = true;
				excelItem.setShipmentnumber(trayCode+"-"+"RK-"+cn.stylefeng.guns.core.util.DateUtil.getDateBch()+"-"+(putNumber));
			}
			opp.setPutCustomerNumber(excelItem.getShipmentnumber());//客户单号 (客户货运订单号)
			OrderPutResult oprOrderPutResult = this.findBySpec(opp,null);//状态不等于 作废
			//如果不存在
			if(oprOrderPutResult==null) {
				//新增入库订单
				opp.setPutNumber(trayCode+"-"+"RK-"+cn.stylefeng.guns.core.util.DateUtil.getDateBch()+"-"+(putNumber));
		    	opp.setPutCustomerId(orderPutParam.getPutCustomerId());//客户id
		    	opp.setPutCustomerName(basicCustomer.getCustomerName());//客户名称
		    	opp.setSettlementunit(basicCustomer.getCustomerSettlement());//结算单位
		    	opp.setWarehouseId(orderPutParam.getWarehouseId());//仓库id
		    	opp.setWarehouseName(basicWarehouse.getWarehouseName());//仓库名称
		    	opp.setPutType("RETURN_STORAGE");//入库类别 默认为‘提货入库’
		    	opp.setPutMode("WINDOW_RECEIVING");//入库方式 默认为‘窗口接货’  不生成货运订单
		    	opp.setSettlement(orderPutParam.getSettlement());//结算方式
		    	opp.setSettlementunit(null);
		    	opp.setCarnumber(excelItem.getCarNumber());//车牌号
		    	opp.setShipmentnumber(excelItem.getPutCustomerNumber());//客户交货单号
		    	opp.setBillstate("PENDING_SUBMIT");//订单状态
		    	opp.setCreateName(user.getName());//制单人名称
		    	opp.setCreateTime(new Date());
		    	opp.setCreateUser(user.getUserId());
		    	opp.setNotes(excelItem.getNotes());
		    	opp.setDeliverId(deliverId);//交付id
		    	opp.setDeptId(deptId);//部门id
		    	boolean whether = false;//判断客户单号  是否重复
		    	for (OrderPutParam opp2 : orderPutList) {
					if (opp2.getPutCustomerNumber().equals(opp.getPutCustomerNumber())) {
						whether = true;
				    	oprOrderPutResult = new OrderPutResult();
		    			ToolUtil.copyProperties(opp2,oprOrderPutResult);
					}
				}
		    	if (!whether) {
		    		orderPutList.add(opp);
		    		if(!whether2) {
		    			putNumber++;
		    		}
			    	oprOrderPutResult = new OrderPutResult();
	    			ToolUtil.copyProperties(opp,oprOrderPutResult);
				}
    			opp = new OrderPutParam();
			} else {
				//如果 导入的客户单号 已存在
				continue;
			}
			
			//通过商品编号 查询商品是否存在
			BasicGoodsParam bgpParam = new BasicGoodsParam();
			bgpParam.setGoodsNumber(excelItem.getGoodsNumber());
			bgpParam.setGoodsCustomerId(Integer.parseInt(orderPutParam.getPutCustomerId().toString()));
			BasicGoodsResult bgrBasicGoodsResult = basicGoodsService.findBySpec(bgpParam,null);
			//计算 单价，体积单位
			BigDecimal putvolume =  new BigDecimal(0);//应入体积
			BigDecimal v = new BigDecimal(0);//体积单位
			BigDecimal u = new BigDecimal(0);//单价
			if (ToolUtil.isNotEmpty(excelItem.getPutvolume())) {
				putvolume =  new BigDecimal(excelItem.getPutvolume()).setScale(3, BigDecimal.ROUND_HALF_DOWN);
				v = putvolume.divide(putnum,8,BigDecimal.ROUND_HALF_DOWN);//(四舍五入保留8位)
			} else {
				if (bgrBasicGoodsResult != null && ToolUtil.isNotEmpty(bgrBasicGoodsResult.getGoodsVolume())) {
					v = new BigDecimal(bgrBasicGoodsResult.getGoodsVolume()).setScale(8, BigDecimal.ROUND_HALF_DOWN);//(四舍五入保留8位)
					putvolume =  putnum.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN);
				} else {
					v =  new BigDecimal("0.1");
				}
			}
			BigDecimal detailedMoney = new BigDecimal(0);//金额
			if (ToolUtil.isNotEmpty(excelItem.getDetailedMoney())) {
				detailedMoney = new BigDecimal(excelItem.getDetailedMoney()).setScale(3, BigDecimal.ROUND_HALF_DOWN);
				u = detailedMoney.divide(putnum,3,BigDecimal.ROUND_HALF_DOWN);//(四舍五入保留3位)
			} else {
				if (bgrBasicGoodsResult != null && ToolUtil.isNotEmpty(bgrBasicGoodsResult.getGoodsCharging())) {
					u = new BigDecimal(bgrBasicGoodsResult.getGoodsCharging()).setScale(3, BigDecimal.ROUND_HALF_DOWN);
					detailedMoney = putnum.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN);
				} else {
					u = new BigDecimal("10");
				}
			}
			//如果商品不存在  新增基础信息
			if(bgrBasicGoodsResult==null){
				bgpParam.setGoodsCustomerName(basicCustomer.getCustomerName());//客户名称
				bgpParam.setGoodsName(excelItem.getGoodsName());//商品名称
				bgpParam.setGoodsUnit(excelItem.getGoodsUnit());//单位 (基本单位)
				bgpParam.setGoodsBigtype(excelItem.getGoodsType());//商品大类 (品类描述)
				bgpParam.setGoodsWeight("0.1");//重量单位 默认1
				if (v.compareTo(as)==1) {//大于 返回1
					bgpParam.setGoodsVolume(v.toString());//体积
				} else {
					bgpParam.setGoodsVolume("0.1");//默认1
				}
				if(u.compareTo(as)==1) {//大于 返回1
					bgpParam.setGoodsCharging(u.toString());//计费指定价
				} else {
					bgpParam.setGoodsCharging("10");//默认1
				}
				bgpParam.setGoodsBottom("10");//底
				bgpParam.setGoodsLamination("10");//叠层
				bgpParam.setGoodsSupportvolume("10000");//每托存量 默认100
				bgpParam.setGoodsTraylayer("1");//托层 默认1
				bgpParam.setGoodsWarranty("36");
				bgpParam.setCreateTime(new Date());
				bgpParam.setDeliverId(deliverId);
				bgpParam.setDeptId(deptId);
				basicGoodsService.addReturnId(bgpParam);
				bgrBasicGoodsResult = new BasicGoodsResult();
				ToolUtil.copyProperties(bgpParam, bgrBasicGoodsResult);
				newgoodsflag = true;
				goodsnumbers += excelItem.getGoodsNumber()+";";
			} else {
				boolean flag = false;//是否更新商品
				if (ToolUtil.isNotEmpty(bgrBasicGoodsResult.getGoodsVolume())) {
					BigDecimal volume =  new BigDecimal(bgrBasicGoodsResult.getGoodsVolume());
					if(v.compareTo(volume) != 0) {
						bgrBasicGoodsResult.setGoodsVolume(v.toString());
						flag = true;
					}
				} else {
					bgrBasicGoodsResult.setGoodsVolume(v.toString());
					flag = true;
				}
				if (ToolUtil.isNotEmpty(bgrBasicGoodsResult.getGoodsCharging())) {
					BigDecimal charging =  new BigDecimal(bgrBasicGoodsResult.getGoodsCharging());
					if(u.compareTo(charging) != 0) {
						bgrBasicGoodsResult.setGoodsCharging(u.toString());
						flag = true;
					}
				} else {
					bgrBasicGoodsResult.setGoodsCharging(u.toString());
					flag = true;
				}
				if (flag) {
					ToolUtil.copyProperties(bgrBasicGoodsResult, bgpParam);
					basicGoodsService.update(bgpParam);
				}
			}
			BigDecimal goodsWeight = new BigDecimal(bgrBasicGoodsResult.getGoodsWeight());//重量单位
			String batchnumber = excelItem.getBatchnumber();//导入生产日期
			if(ToolUtil.isEmpty(batchnumber)){
				batchnumber = cn.stylefeng.guns.core.util.DateUtil.getDateBch();
			}
			char[] dataArray = batchnumber.toCharArray();
			StringBuffer strBuffer = new StringBuffer();
			if(batchnumber.length()==10) {
				batchnumber = "20"+batchnumber.substring(0,6);//恒安导入 2111120456
			} else if(batchnumber.length()==19){
				batchnumber = batchnumber.substring(0, 10).replace("-","");
			} else {
				batchnumber = batchnumber.substring(0, 8);//贝因美导入 20210613
			}
			dataArray = batchnumber.toCharArray();
			for(int i = 0; i < dataArray.length; i++){
				strBuffer.append(dataArray[i]);
				if(i == 3 || i == 5){
					strBuffer.append("-");
				}
			}
			
			if(ToolUtil.isNotEmpty(excelItem.getGoodsbatchs())) {
				batchnumber = excelItem.getGoodsbatchs();
			}
			
			String recommend = getDatesssss();//入库批次
			if (ToolUtil.isNotEmpty(excelItem.getRecommend())) {
				char[] dataArray2 = excelItem.getRecommend().toCharArray();
				StringBuffer strBuffer2 = new StringBuffer();
				for(int i = 0; i < dataArray2.length; i++){
					strBuffer2.append(dataArray2[i]);
					if(i == 3 || i == 5){
						strBuffer2.append("-");
					}
				}
				recommend = strBuffer2.toString();
			}
			//创建订单明细
			OrderPutDetailedParam opdpDetailedParam = new OrderPutDetailedParam();
			opdpDetailedParam.setPutDetailedNumber(oprOrderPutResult.getPutNumber());//入库订单编号
			opdpDetailedParam.setDetailedGoodsname(bgrBasicGoodsResult.getGoodsName());//商品名称
			opdpDetailedParam.setDetailedGoodsnum(Long.parseLong(bgrBasicGoodsResult.getId().toString()));//商品id
			opdpDetailedParam.setGoodsNumber(bgrBasicGoodsResult.getGoodsNumber());//商品编号
			opdpDetailedParam.setDetailedPutbalenum(putnum.toString());//应入大包数
			opdpDetailedParam.setDetailedPutnum(putnum.toString());//应入数量
			opdpDetailedParam.setDetailedPutweight(putnum.multiply(goodsWeight).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//应入重量
			opdpDetailedParam.setGoodsWeight(goodsWeight.toString());//重量
			opdpDetailedParam.setDetailedPutvolume(putnum.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//应入体积
			opdpDetailedParam.setGoodsVolume(v.toString());//体积
			opdpDetailedParam.setDetailedUnitprice(u.toString());//单价
			opdpDetailedParam.setDetailedMoney(putnum.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//金额
			opdpDetailedParam.setDetailedBatchnumber(batchnumber);//批次
			opdpDetailedParam.setDetailedDateofmanufacture(strBuffer.toString());//生产日期
			opdpDetailedParam.setRecommend(recommend);//推荐批次
			opdpDetailedParam.setDetailedRebubbles(bgrBasicGoodsResult.getGoodsHeavybubble());//重泡货
			opdpDetailedParam.setDetailedPacking(bgrBasicGoodsResult.getGoodsPacking());//包装
			opdpDetailedParam.setDetailedLargeclass(bgrBasicGoodsResult.getGoodsBigtype());//商品大类 (导入品类描述)
			opdpDetailedParam.setDetailedGoodstype(bgrBasicGoodsResult.getGoodsType());//商品类别
			opdpDetailedParam.setGoodsNo(bgrBasicGoodsResult.getGoodsNo());
			if(ToolUtil.isNotEmpty(excelItem.getGoodsUnit())) {
				opdpDetailedParam.setGoodsUnit(excelItem.getGoodsUnit());
			} else {
				opdpDetailedParam.setGoodsUnit(bgrBasicGoodsResult.getGoodsUnit());
			}
			opdpDetailedParam.setGoodsSpecs(bgrBasicGoodsResult.getGoodsSpecs());
			opdpDetailedParam.setCreateTime(new Date());
			detailedList.add(opdpDetailedParam);
			opdpDetailedParam = new OrderPutDetailedParam();
		}
		if(detailedList.size()>0){
			this.saveBatchList(orderPutList);
			orderPutDetailedService.saveBatchList(detailedList);
		}
		data.setSuccess(true);
		if (newgoodsflag) {
			data.setCode(450);
			data.setMessage("New product code: "+goodsnumbers+" Please maintain and correct the basic product information before submitting the warehousing order!");
		}
		return data;
	}

	@Override
	public Workbook exportToExcel(OrderPutParam orderPutParam) {
	    //这是表头
	    String[] arr = {"Status","Customer","Warehouse","System Order Number","Customer Order Number","Warehouse Entry Category","Warehouse Entry Method","Quantity"
				,"Volume","Weight","Amount","Order Maker","Order Time","Submitter","Submit Time"
				,"Warehouse Entry Complete Time","Remarks"};
	    //这是具体数据
	    List<List<String>> list = new ArrayList<>();

    	User user = userService.getcurrentUser();
    	orderPutParam.setDeptId(user.getDeptId());
	    List<OrderPutResult> datalist = this.findListBySpec(orderPutParam,user.getOperateId());
	    for (OrderPutResult r : datalist) {
		    List<String> list1 = new ArrayList<String>();
		    list1.add(r.getBillstate());
		    list1.add(r.getPutCustomerName());
		    list1.add(r.getWarehouseName());
		    list1.add(r.getPutNumber());
		    list1.add(r.getPutCustomerNumber());
		    list1.add(r.getPutType());
		    list1.add(r.getPutMode());
		    list1.add(r.getDetailedPutnum());
		    list1.add(r.getDetailedPutvolume());
		    list1.add(r.getDetailedPutweight());
		    list1.add(r.getDetailedMoney());
		    list1.add(r.getCreateName());
	    	list1.add(r.getCreateTimestr());
		    list1.add(r.getSubmissionName());
	    	list1.add(r.getSubmissionTimestr());
	    	list1.add(r.getCompleteTimestr());
		    list1.add(r.getNotes());
		    list.add(list1);
		}
	    return ExcelWrite.writeToExcelByList(arr, list);
	}

	@Override
	@Transactional(rollbackFor = Exception.class,timeout=80)
	public ResponseData getExceldataLY(MultipartFile file, OrderPutParam orderPutParam, ResponseData data)  throws Exception{

		String fileName = file.getOriginalFilename();
		List<OrderPutExcelItem> list = new ArrayList<OrderPutExcelItem>();

		if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
			data.setMessage("Incorrect upload file format");
			data.setSuccess(false);
		}

		boolean isExcel2003 = true;
		if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
			isExcel2003 = false;
		}
		InputStream is = file.getInputStream();
		Workbook wb = null;
		if (isExcel2003) {
			wb = new HSSFWorkbook(is);
		} else {
			wb = new XSSFWorkbook(is);
		}
		Sheet sheet = wb.getSheetAt(0);

		for (int r = 1; r <= sheet.getLastRowNum()+1; r++) {
			Row row = sheet.getRow(r);
			if (row == null){
				continue;
			}
			OrderPutExcelItem excelItem = new OrderPutExcelItem();

//			String  carNumber = getCellValue(row.getCell(0));
//			if (ToolUtil.isNotEmpty(carNumber)) {
//				excelItem.setCarNumber(carNumber);//车牌号
//			}

			String goodsNumber = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(4));
			if (ToolUtil.isNotEmpty(goodsNumber)) {
				excelItem.setGoodsNumber(goodsNumber);//商品编号
			}

			String goodsName = getCellValue(row.getCell(5));
			if (ToolUtil.isNotEmpty(goodsName)) {
				excelItem.setGoodsName(goodsName);//商品名称
			}

			String batchnumber = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(13));
			if (ToolUtil.isNotEmpty(batchnumber)) {
				excelItem.setBatchnumber(batchnumber);//生产日期
			}

			String putnum = getCellValue(row.getCell(6));
			if (ToolUtil.isNotEmpty(putnum)) {
				BigDecimal as = new BigDecimal(0);
				BigDecimal putnumb = new BigDecimal(putnum);
				if(putnumb.compareTo(as) != 1) {
					continue;
				}
				excelItem.setPutnum(putnum);//应入数量
			}

//			String detailedMoney = getCellValue(row.getCell(5));
//			if (ToolUtil.isNotEmpty(detailedMoney)) {
//				excelItem.setDetailedMoney(detailedMoney);//金额
//			}

//			String putvolume = getCellValue(row.getCell(6));
//			if (ToolUtil.isNotEmpty(putvolume)) {
//				excelItem.setPutvolume(putvolume);//应入体积
//			}

			String shipmentnumber = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(1));
			if (ToolUtil.isNotEmpty(shipmentnumber)) {
				excelItem.setShipmentnumber(shipmentnumber);//客户货运订单号   (客户单号){
			}

//			String putCustomerNumber = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(8));
//			if (ToolUtil.isNotEmpty(putCustomerNumber)) {
//				excelItem.setPutCustomerNumber(putCustomerNumber);//客户交货单号
//			}

//			String goodsType = getCellValue(row.getCell(9));
//			if (ToolUtil.isNotEmpty(goodsType)) {
//				excelItem.setGoodsType(goodsType);//品类描述
//			}

			String goodsUnit = getCellValue(row.getCell(7));
			if (ToolUtil.isNotEmpty(goodsUnit)) {
				excelItem.setGoodsUnit(goodsUnit);//基本单位
			}
//
//			String recommend = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(11));
//			if (ToolUtil.isNotEmpty(recommend)) {
//				excelItem.setRecommend(recommend);//推荐批次
//			}
//
//			String notes = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(12));
//			if (ToolUtil.isNotEmpty(notes)) {
//				excelItem.setNotes(notes);
//			}
//
//			String goodsbatchs = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(13));
//			if (ToolUtil.isNotEmpty(goodsbatchs)) {
//				excelItem.setGoodsbatchs(goodsbatchs);
//			}
//
//			String goodsSpecs = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(14));
//			if (ToolUtil.isNotEmpty(goodsSpecs)) {
//				excelItem.setGoodsSpecs(goodsSpecs);
//			}

			list.add(excelItem);
		}

		if(orderPutParam.getWarehouseId()==null||orderPutParam.getPutCustomerId()==null) {
			data.setSuccess(false);
			data.setMessage("Please fill in the order basic information!");
			return data;
		}
		List<OrderPutParam> orderPutList = new ArrayList<OrderPutParam>();//批量保存的 入库订单list
		List<OrderPutDetailedParam> detailedList = new ArrayList<OrderPutDetailedParam>();//批量保存的 订单商品明细list
		BasicCustomer basicCustomer = basicCustomerService.getById(orderPutParam.getPutCustomerId());//客户信息
		BasicWarehouse basicWarehouse = basicWarehouseService.getById(orderPutParam.getWarehouseId());//仓库信息
		//查询最新 入库订单
		OrderPut orderPut = this.findByThelastone();
		int putNumber = 1;
		if (orderPut!=null) {
			putNumber = orderPut.getId()+1;
		}
		boolean newgoodsflag = false;
		String goodsnumbers = "";
		User user = userService.getcurrentUser();
		Long deptId = user.getDeptId();
		Long deliverId = user.getDeliverId();
		String trayCode = getOperateIdOrderPrefix(user);
		for (OrderPutExcelItem excelItem : list) {
			if (ToolUtil.isEmpty(excelItem.getGoodsNumber())&&ToolUtil.isEmpty(excelItem.getGoodsName())) {
				break;
			}
			if(ToolUtil.isEmpty(excelItem.getGoodsNumber())) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
				data.setSuccess(false);
				data.setMessage("Product code cannot be empty in imported data!");
				return data;
			}
			if(ToolUtil.isEmpty(excelItem.getPutnum())) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
				data.setSuccess(false);
				data.setMessage("Inbound quantity cannot be empty in imported data!");
				return data;
			}
			//通过 导入的客户单号 查询是否存在
			OrderPutParam opp = new OrderPutParam();
			boolean whether2 = false;
			if(ToolUtil.isEmpty(excelItem.getShipmentnumber())) {
				whether2 = true;
				excelItem.setShipmentnumber(trayCode+"-"+"RK-"+cn.stylefeng.guns.core.util.DateUtil.getDateBch()+"-"+(putNumber));
			}
			opp.setPutCustomerNumber(excelItem.getShipmentnumber());//客户单号 (客户货运订单号)
			OrderPutResult oprOrderPutResult = this.findBySpec(opp,null);//状态不等于 作废
			//如果不存在
			if(oprOrderPutResult==null) {
				//新增入库订单
				opp.setPutNumber(trayCode+"-"+"RK-"+cn.stylefeng.guns.core.util.DateUtil.getDateBch()+"-"+(putNumber));
				opp.setPutCustomerId(orderPutParam.getPutCustomerId());//客户id
				opp.setPutCustomerName(basicCustomer.getCustomerName());//客户名称
				opp.setSettlementunit(basicCustomer.getCustomerSettlement());//结算单位
				opp.setWarehouseId(orderPutParam.getWarehouseId());//仓库id
				opp.setWarehouseName(basicWarehouse.getWarehouseName());//仓库名称
				opp.setPutType("PICKUP_STORAGE");//入库类别 默认为‘提货入库’
	opp.setPutMode("WINDOW_RECEIVING");//入库方式 默认为‘窗口接货’  不生成货运订单
				opp.setSettlement(orderPutParam.getSettlement());//结算方式
				opp.setSettlementunit(null);
				opp.setCarnumber(excelItem.getCarNumber());//车牌号
				opp.setShipmentnumber(excelItem.getPutCustomerNumber());//客户交货单号
				opp.setBillstate("PENDING_SUBMIT");//订单状态
				opp.setCreateName(user.getName());//制单人名称
				opp.setCreateTime(new Date());
				opp.setCreateUser(user.getUserId());
				opp.setNotes(excelItem.getNotes());
				opp.setDeliverId(deliverId);//交付id
				opp.setDeptId(deptId);//部门id
				boolean whether = false;//判断客户单号  是否重复
				for (OrderPutParam opp2 : orderPutList) {
					if (opp2.getPutCustomerNumber().equals(opp.getPutCustomerNumber())) {
						whether = true;
						oprOrderPutResult = new OrderPutResult();
						ToolUtil.copyProperties(opp2,oprOrderPutResult);
					}
				}
				if (!whether) {
					orderPutList.add(opp);
					if(!whether2) {
						putNumber++;
					}
					oprOrderPutResult = new OrderPutResult();
					ToolUtil.copyProperties(opp,oprOrderPutResult);
				}
				opp = new OrderPutParam();
			} else {
				//如果 导入的客户单号 已存在
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
				data.setSuccess(false);
				data.setMessage("Customer order number: "+excelItem.getShipmentnumber()+" already exists in imported data!");
				return data;
			}

			//通过商品编号 查询商品是否存在
			BasicGoodsParam bgpParam = new BasicGoodsParam();
			bgpParam.setGoodsNumber(excelItem.getGoodsNumber());
			bgpParam.setGoodsCustomerId(Integer.parseInt(orderPutParam.getPutCustomerId().toString()));
			BasicGoodsResult bgrBasicGoodsResult = basicGoodsService.findBySpec(bgpParam,null);
			//计算 单价，体积单位
			BigDecimal putnum = new BigDecimal(excelItem.getPutnum()).setScale(3, BigDecimal.ROUND_HALF_DOWN);//应入数量
			BigDecimal putvolume =  new BigDecimal(0);//应入体积
			BigDecimal v = new BigDecimal(0);//体积单位
			BigDecimal u = new BigDecimal(0);//单价
			if (ToolUtil.isNotEmpty(excelItem.getPutvolume())) {
				putvolume =  new BigDecimal(excelItem.getPutvolume()).setScale(3, BigDecimal.ROUND_HALF_DOWN);
				v = putvolume.divide(putnum,8,BigDecimal.ROUND_HALF_DOWN);//(四舍五入保留8位)
			} else {
				if (bgrBasicGoodsResult != null && ToolUtil.isNotEmpty(bgrBasicGoodsResult.getGoodsVolume())) {
					v = new BigDecimal(bgrBasicGoodsResult.getGoodsVolume()).setScale(8, BigDecimal.ROUND_HALF_DOWN);//(四舍五入保留8位)
					putvolume =  putnum.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN);
				} else {
					v =  new BigDecimal("0.1");
				}
			}
			BigDecimal detailedMoney = new BigDecimal(0);//金额
			if (ToolUtil.isNotEmpty(excelItem.getDetailedMoney())) {
				detailedMoney = new BigDecimal(excelItem.getDetailedMoney()).setScale(3, BigDecimal.ROUND_HALF_DOWN);
				u = detailedMoney.divide(putnum,3,BigDecimal.ROUND_HALF_DOWN);//(四舍五入保留3位)
			} else {
				if (bgrBasicGoodsResult != null && ToolUtil.isNotEmpty(bgrBasicGoodsResult.getGoodsCharging())) {
					u = new BigDecimal(bgrBasicGoodsResult.getGoodsCharging()).setScale(3, BigDecimal.ROUND_HALF_DOWN);
					detailedMoney = putnum.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN);
				} else {
					u = new BigDecimal("0.1");
				}
			}
			BigDecimal as = new BigDecimal("0");
			//如果商品不存在  新增基础信息
			if(bgrBasicGoodsResult==null){
				bgpParam.setGoodsCustomerName(basicCustomer.getCustomerName());//客户名称
				bgpParam.setGoodsName(excelItem.getGoodsName());//商品名称
				bgpParam.setGoodsNo(excelItem.getGoodsNumber());
				bgpParam.setGoodsUnit(excelItem.getGoodsUnit());//单位 (基本单位)
				bgpParam.setGoodsBigtype(excelItem.getGoodsType());//商品大类 (品类描述)
				bgpParam.setGoodsWeight("0.1");//重量单位 默认1
				if (v.compareTo(as)==1) {//大于 返回1
					bgpParam.setGoodsVolume(v.toString());//体积
				} else {
					bgpParam.setGoodsVolume("0.1");//默认1
				}
				if(u.compareTo(as)==1) {//大于 返回1
					bgpParam.setGoodsCharging(u.toString());//计费指定价
				} else {
					bgpParam.setGoodsCharging("10");//默认1
				}
				bgpParam.setGoodsBottom("10");//底
				bgpParam.setGoodsLamination("10");//叠层
				bgpParam.setGoodsSupportvolume("100");//每托存量 默认100
				bgpParam.setGoodsTraylayer("1");//托层 默认1
				bgpParam.setGoodsWarranty("36");
				bgpParam.setGoodsSpecs(excelItem.getGoodsSpecs());
				bgpParam.setDeliverId(deliverId);
				bgpParam.setDeptId(deptId);
				bgpParam.setCreateTime(new Date());
				basicGoodsService.addReturnId(bgpParam);
				bgrBasicGoodsResult = new BasicGoodsResult();
				ToolUtil.copyProperties(bgpParam, bgrBasicGoodsResult);
				newgoodsflag = true;
				goodsnumbers += excelItem.getGoodsNumber()+";";
			} else {
				boolean flag = false;//是否更新商品
				if (ToolUtil.isNotEmpty(bgrBasicGoodsResult.getGoodsVolume())) {
					BigDecimal volume =  new BigDecimal(bgrBasicGoodsResult.getGoodsVolume());
					if(v.compareTo(volume) != 0) {
						bgrBasicGoodsResult.setGoodsVolume(v.toString());
						flag = true;
					}
				} else {
					bgrBasicGoodsResult.setGoodsVolume(v.toString());
					flag = true;
				}
				if (ToolUtil.isNotEmpty(bgrBasicGoodsResult.getGoodsCharging())) {
					BigDecimal charging =  new BigDecimal(bgrBasicGoodsResult.getGoodsCharging());
					if(u.compareTo(charging) != 0) {
						bgrBasicGoodsResult.setGoodsCharging(u.toString());
						flag = true;
					}
				} else {
					bgrBasicGoodsResult.setGoodsCharging(u.toString());
					flag = true;
				}
				if (flag) {
					ToolUtil.copyProperties(bgrBasicGoodsResult, bgpParam);
					basicGoodsService.update(bgpParam);
				}
			}
			BigDecimal goodsWeight = new BigDecimal(bgrBasicGoodsResult.getGoodsWeight());//重量单位
			String batchnumber = excelItem.getBatchnumber();//导入生产日期
			if(ToolUtil.isEmpty(batchnumber)){
				batchnumber = cn.stylefeng.guns.core.util.DateUtil.getDateBch();
			}
			char[] dataArray = batchnumber.toCharArray();
			StringBuffer strBuffer = new StringBuffer();
//			if(batchnumber.length()==10) {
//				batchnumber = "20"+batchnumber.substring(0,6);//恒安导入 2111120456
//			} else if(batchnumber.length()==19){
//				batchnumber = batchnumber.substring(0, 10).replace("-","");
//			} else {
//				batchnumber = batchnumber.substring(0, 8);//贝因美导入 20210613
//			}
			dataArray = batchnumber.toCharArray();
			for(int i = 0; i < dataArray.length; i++){
				strBuffer.append(dataArray[i]);
				if(i == 3 || i == 5){
					strBuffer.append("-");
				}
			}

			if(ToolUtil.isNotEmpty(excelItem.getGoodsbatchs())) {
				batchnumber = excelItem.getGoodsbatchs();
			}

			String recommend = getDatesssss();//入库批次
			if (ToolUtil.isNotEmpty(excelItem.getRecommend())) {
				char[] dataArray2 = excelItem.getRecommend().toCharArray();
				StringBuffer strBuffer2 = new StringBuffer();
				for(int i = 0; i < dataArray2.length; i++){
					strBuffer2.append(dataArray2[i]);
					if(i == 3 || i == 5){
						strBuffer2.append("-");
					}
				}
				recommend = strBuffer2.toString();
			}
			//创建订单明细
			OrderPutDetailedParam opdpDetailedParam = new OrderPutDetailedParam();
			opdpDetailedParam.setPutDetailedNumber(oprOrderPutResult.getPutNumber());//入库订单编号
			opdpDetailedParam.setDetailedGoodsname(bgrBasicGoodsResult.getGoodsName());//商品名称
			opdpDetailedParam.setDetailedGoodsnum(Long.parseLong(bgrBasicGoodsResult.getId().toString()));//商品id
			opdpDetailedParam.setGoodsNumber(bgrBasicGoodsResult.getGoodsNumber());//商品编号
			opdpDetailedParam.setGoodsNo(bgrBasicGoodsResult.getGoodsNo());//货号
			opdpDetailedParam.setDetailedPutbalenum(putnum.toString());//应入大包数
			opdpDetailedParam.setDetailedPutnum(putnum.toString());//应入数量
			opdpDetailedParam.setDetailedPutweight(putnum.multiply(goodsWeight).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//应入重量
			opdpDetailedParam.setGoodsWeight(goodsWeight.toString());//重量
			opdpDetailedParam.setDetailedPutvolume(putnum.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//应入体积
			opdpDetailedParam.setGoodsVolume(v.toString());//体积
			opdpDetailedParam.setDetailedUnitprice(u.toString());//单价
			opdpDetailedParam.setDetailedMoney(putnum.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//金额
			opdpDetailedParam.setDetailedBatchnumber(batchnumber);//批次
			opdpDetailedParam.setDetailedDateofmanufacture(strBuffer.toString());//生产日期
			opdpDetailedParam.setRecommend(recommend);//推荐批次
			opdpDetailedParam.setDetailedRebubbles(bgrBasicGoodsResult.getGoodsHeavybubble());//重泡货
			opdpDetailedParam.setDetailedLargeclass(bgrBasicGoodsResult.getGoodsBigtype());//大类 (导入品类描述)
			opdpDetailedParam.setDetailedGoodstype(bgrBasicGoodsResult.getGoodsType());//类别
			if(ToolUtil.isNotEmpty(excelItem.getGoodsUnit())) {
				opdpDetailedParam.setGoodsUnit(excelItem.getGoodsUnit());
			} else {
				opdpDetailedParam.setGoodsUnit(bgrBasicGoodsResult.getGoodsUnit());//单位
			}
			opdpDetailedParam.setDetailedPacking(bgrBasicGoodsResult.getGoodsPacking());//包装
			opdpDetailedParam.setGoodsSpecs(bgrBasicGoodsResult.getGoodsSpecs());//规格
			opdpDetailedParam.setCreateTime(new Date());
			detailedList.add(opdpDetailedParam);
			opdpDetailedParam = new OrderPutDetailedParam();
		}
		if(detailedList.size()>0){
			this.saveBatchList(orderPutList);
			orderPutDetailedService.saveBatchList(detailedList);
		}
		data.setSuccess(true);
		if (newgoodsflag) {
			data.setCode(450);
			data.setMessage("New product code: "+goodsnumbers+" Please maintain and correct the basic product information before submitting the warehousing order!");
		}
		return data;
	}

}
